mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
Compare commits
71
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0343656cdb | ||
|
|
f08f075d23 | ||
|
|
7c980774e7 | ||
|
|
8f6121694a | ||
|
|
137d29633b | ||
|
|
ca0314ea4f | ||
|
|
3b5fcae07f | ||
|
|
82f0a82564 | ||
|
|
7603528aff | ||
|
|
cec96c02ea | ||
|
|
ee2718b14c | ||
|
|
7ff77a4f3b | ||
|
|
0b197867a1 | ||
|
|
1451096c17 | ||
|
|
4854c6a0f4 | ||
|
|
74da88fc29 | ||
|
|
744ab586c5 | ||
|
|
364e19a493 | ||
|
|
df33e81915 | ||
|
|
867a537802 | ||
|
|
85c1b990b1 | ||
|
|
ac319fe318 | ||
|
|
164a7d8b56 | ||
|
|
d35f4c1c6b | ||
|
|
1aad4cfe70 | ||
|
|
ea859a0844 | ||
|
|
c0152e6be1 | ||
|
|
e057244b0a | ||
|
|
763e2a2090 | ||
|
|
917b6f4afd | ||
|
|
106fd31af1 | ||
|
|
137f23a229 | ||
|
|
e645449872 | ||
|
|
9b642d4636 | ||
|
|
a882565cd7 | ||
|
|
de2fc03c7d | ||
|
|
21add33b6e | ||
|
|
9a03c09396 | ||
|
|
f40267db44 | ||
|
|
6ee7c4c5c7 | ||
|
|
9c427b265f | ||
|
|
97f8ca13ff | ||
|
|
780760fccc | ||
|
|
a3e97ec709 | ||
|
|
23d8c62844 | ||
|
|
4bdbfbaaa8 | ||
|
|
e73c23a4ee | ||
|
|
8c3d8fcea0 | ||
|
|
bb1ba8b247 | ||
|
|
593cfee85b | ||
|
|
03065155db | ||
|
|
f8cfea0dcf | ||
|
|
c90da999f2 | ||
|
|
379abbe278 | ||
|
|
c601712cc4 | ||
|
|
ab738bfd33 | ||
|
|
222e96d6ab | ||
|
|
a724f4296c | ||
|
|
f05d743459 | ||
|
|
03a75cbc52 | ||
|
|
efa9d304d2 | ||
|
|
b7f5d04428 | ||
|
|
ece2f7a7c2 | ||
|
|
610346ae8c | ||
|
|
df89afbef4 | ||
|
|
1253b97303 | ||
|
|
a1d8739464 | ||
|
|
1cfd9bbb4c | ||
|
|
0de6347b11 | ||
|
|
6936bbdc3e | ||
|
|
4d06742664 |
No files matched your search
@@ -0,0 +1,777 @@
|
||||
name: POSIX Compliance Tests
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main, master, develop ]
|
||||
paths:
|
||||
- 'weed/mount/**'
|
||||
- 'weed/command/mount*'
|
||||
- 'weed/command/fuse*'
|
||||
- 'test/fuse_integration/**'
|
||||
pull_request:
|
||||
branches: [ main, master, develop ]
|
||||
paths:
|
||||
- 'weed/mount/**'
|
||||
- 'weed/command/mount*'
|
||||
- 'weed/command/fuse*'
|
||||
- 'test/fuse_integration/**'
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
test_type:
|
||||
description: 'Type of POSIX tests to run (default: full for comprehensive testing)'
|
||||
required: true
|
||||
default: 'full'
|
||||
type: choice
|
||||
options:
|
||||
- critical
|
||||
- basic
|
||||
- extended
|
||||
- full
|
||||
enable_external_tests:
|
||||
description: 'Run external test suites (slower)'
|
||||
required: false
|
||||
default: false
|
||||
type: boolean
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
pull-requests: write
|
||||
issues: write
|
||||
|
||||
env:
|
||||
GO_VERSION: '1.26'
|
||||
TIMEOUT: '45m'
|
||||
|
||||
jobs:
|
||||
posix-compliance-ubuntu:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 60
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
fuse-version: ['2.9', '3.0']
|
||||
fail-fast: false
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v7
|
||||
with:
|
||||
fetch-depth: 1
|
||||
|
||||
- name: Set up Go ${{ env.GO_VERSION }}
|
||||
uses: actions/setup-go@v7
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
cache: true
|
||||
cache-dependency-path: |
|
||||
go.sum
|
||||
test/fuse_integration/go.sum
|
||||
|
||||
- name: Install system dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y \
|
||||
attr \
|
||||
acl \
|
||||
build-essential \
|
||||
git \
|
||||
python3-pip
|
||||
|
||||
# Install FUSE version specific packages
|
||||
if [ "${{ matrix.fuse-version }}" = "2.9" ]; then
|
||||
echo "Installing FUSE 2.9..."
|
||||
sudo apt-get install -y fuse libfuse-dev
|
||||
elif [ "${{ matrix.fuse-version }}" = "3.0" ]; then
|
||||
echo "Installing FUSE 3.0..."
|
||||
sudo apt-get install -y fuse3 libfuse3-dev
|
||||
# Also install fuse2 for compatibility if needed
|
||||
sudo apt-get install -y fuse libfuse-dev
|
||||
fi
|
||||
|
||||
- name: Set up user permissions for FUSE
|
||||
run: |
|
||||
# Create fuse group if it doesn't exist
|
||||
sudo groupadd -f fuse
|
||||
|
||||
# Add user to fuse group
|
||||
sudo usermod -a -G fuse $USER
|
||||
|
||||
# Set permissions on FUSE device
|
||||
sudo chmod 666 /dev/fuse
|
||||
|
||||
# Ensure fuse module is loaded
|
||||
sudo modprobe fuse || true
|
||||
|
||||
# Verify setup
|
||||
echo "FUSE setup verification:"
|
||||
ls -la /dev/fuse
|
||||
groups $USER | grep fuse || echo "User not in fuse group yet (will be effective after login)"
|
||||
lsmod | grep fuse || echo "FUSE module not loaded"
|
||||
|
||||
- name: Install external test tools
|
||||
if: ${{ github.event.inputs.enable_external_tests == 'true' }}
|
||||
run: |
|
||||
# Install nfstest for POSIX API verification
|
||||
pip3 install --user nfstest
|
||||
|
||||
# Install FIO for performance testing
|
||||
sudo apt-get install -y fio
|
||||
|
||||
# Install additional test utilities
|
||||
sudo apt-get install -y stress-ng
|
||||
|
||||
- name: Build SeaweedFS
|
||||
run: |
|
||||
make
|
||||
# Verify binary exists and is executable (it's installed to GOPATH/bin)
|
||||
weed version
|
||||
|
||||
# Make weed binary available in PATH (it's already there from go install)
|
||||
sudo cp $(which weed) /usr/local/bin/weed
|
||||
which weed
|
||||
weed version
|
||||
|
||||
- name: Set up SeaweedFS cluster
|
||||
run: |
|
||||
start_and_wait() {
|
||||
local name=$1
|
||||
local url=$2
|
||||
local pidfile=$3
|
||||
shift 3
|
||||
local cmd="$@"
|
||||
|
||||
echo "Starting $name..."
|
||||
$cmd > "/tmp/seaweedfs/$name.log" 2>&1 &
|
||||
local pid=$!
|
||||
echo $pid > "$pidfile"
|
||||
|
||||
echo "Waiting for $name to start..."
|
||||
for i in {1..30}; do
|
||||
if curl -sf "$url" > /dev/null 2>&1; then
|
||||
echo "$name is ready"
|
||||
return 0
|
||||
fi
|
||||
if [ $i -eq 30 ]; then
|
||||
echo "$name failed to start"
|
||||
cat "/tmp/seaweedfs/$name.log"
|
||||
exit 1
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
}
|
||||
|
||||
mkdir -p /tmp/seaweedfs/{master,volume,filer,mount}
|
||||
|
||||
start_and_wait "master" "http://127.0.0.1:9333/cluster/status" "/tmp/seaweedfs/master.pid" \
|
||||
"weed master -ip=127.0.0.1 -port=9333 -mdir=/tmp/seaweedfs/master -raftBootstrap=true"
|
||||
|
||||
start_and_wait "volume" "http://127.0.0.1:8080/status" "/tmp/seaweedfs/volume.pid" \
|
||||
"weed volume -mserver=127.0.0.1:9333 -ip=127.0.0.1 -port=8080 -dir=/tmp/seaweedfs/volume -max=100"
|
||||
|
||||
start_and_wait "filer" "http://127.0.0.1:8888/" "/tmp/seaweedfs/filer.pid" \
|
||||
"weed filer -master=127.0.0.1:9333 -ip=127.0.0.1 -port=8888"
|
||||
|
||||
echo "SeaweedFS cluster status:"
|
||||
curl -s http://127.0.0.1:9333/cluster/status || true
|
||||
|
||||
- name: Set up FUSE mount
|
||||
run: |
|
||||
# Create mount point
|
||||
MOUNT_POINT="/tmp/seaweedfs/mount"
|
||||
mkdir -p $MOUNT_POINT
|
||||
|
||||
echo "Mounting SeaweedFS FUSE filesystem..."
|
||||
# Mount SeaweedFS FUSE filesystem in background
|
||||
weed mount \
|
||||
-filer=127.0.0.1:8888 \
|
||||
-dir=$MOUNT_POINT \
|
||||
-filer.path=/ \
|
||||
-dirAutoCreate=true \
|
||||
-allowOthers=true \
|
||||
-nonempty=true \
|
||||
> /tmp/seaweedfs/mount.log 2>&1 &
|
||||
MOUNT_PID=$!
|
||||
echo $MOUNT_PID > /tmp/seaweedfs/mount.pid
|
||||
|
||||
# Wait for mount to be ready. The mount point directory already
|
||||
# exists, so only a real mount counts — a directory listing would
|
||||
# pass on the local disk even if weed mount died.
|
||||
echo "Waiting for FUSE mount to be ready..."
|
||||
for i in {1..30}; do
|
||||
if mountpoint -q $MOUNT_POINT 2>/dev/null; then
|
||||
echo "FUSE mount is ready"
|
||||
break
|
||||
fi
|
||||
if ! kill -0 $MOUNT_PID 2>/dev/null; then
|
||||
echo "FUSE mount process exited"
|
||||
cat /tmp/seaweedfs/mount.log
|
||||
exit 1
|
||||
fi
|
||||
if [ $i -eq 30 ]; then
|
||||
echo "FUSE mount failed to be ready"
|
||||
cat /tmp/seaweedfs/mount.log
|
||||
echo "Mount status:"
|
||||
mount | grep fuse || echo "No FUSE mounts found"
|
||||
ps aux | grep weed || echo "No weed processes found"
|
||||
exit 1
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
|
||||
# Verify mount is working
|
||||
echo "Testing FUSE mount functionality..."
|
||||
echo "test" > $MOUNT_POINT/test_file.txt
|
||||
if [ "$(cat $MOUNT_POINT/test_file.txt)" = "test" ]; then
|
||||
echo "FUSE mount is working correctly"
|
||||
rm $MOUNT_POINT/test_file.txt
|
||||
else
|
||||
echo "FUSE mount is not working properly"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Export mount point for tests
|
||||
echo "SEAWEEDFS_MOUNT_POINT=$MOUNT_POINT" >> $GITHUB_ENV
|
||||
|
||||
- name: Set up test environment
|
||||
run: |
|
||||
cd test/fuse_integration
|
||||
|
||||
# Initialize Go module for tests
|
||||
if [ ! -f go.mod ]; then
|
||||
go mod init seaweedfs-posix-tests
|
||||
go mod tidy
|
||||
fi
|
||||
|
||||
# Create reports directory
|
||||
mkdir -p reports
|
||||
|
||||
# Set up external tools if requested
|
||||
# Note: External tools setup is now optional and won't fail the build
|
||||
if [ "${{ github.event.inputs.enable_external_tests }}" = "true" ]; then
|
||||
echo "Setting up external POSIX testing tools..."
|
||||
make -f posix_Makefile setup-external-tools || true
|
||||
# setup-external-tools exits 0 even when a sub-target failed, so
|
||||
# require the tools only when they are actually installed
|
||||
if [ -d external_tools/pjdfstest ] && command -v nfstest_posix >/dev/null 2>&1; then
|
||||
export POSIX_REQUIRE_EXTERNAL_TOOLS="true"
|
||||
echo "POSIX_REQUIRE_EXTERNAL_TOOLS=true" >> $GITHUB_ENV
|
||||
echo "✅ External tools setup completed successfully"
|
||||
else
|
||||
echo "⚠️ External tools setup incomplete - continuing with basic tests only"
|
||||
echo "External tools will be skipped but tests will continue"
|
||||
echo "This is expected behavior when build tools are not available"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Verify SeaweedFS cluster is accessible
|
||||
echo "Verifying SeaweedFS cluster accessibility..."
|
||||
curl -sf http://127.0.0.1:9333/cluster/status
|
||||
curl -sf http://127.0.0.1:8080/status
|
||||
curl -sf http://127.0.0.1:8888/
|
||||
|
||||
# Verify mount point
|
||||
echo "Mount point: $SEAWEEDFS_MOUNT_POINT"
|
||||
ls -la $SEAWEEDFS_MOUNT_POINT
|
||||
|
||||
# Verify mount is actually working by testing file operations
|
||||
echo "Verifying SeaweedFS mount functionality..."
|
||||
TEST_FILE="$SEAWEEDFS_MOUNT_POINT/.mount_verification_test"
|
||||
echo "SeaweedFS mount test" > "$TEST_FILE"
|
||||
if [ "$(cat "$TEST_FILE")" = "SeaweedFS mount test" ]; then
|
||||
echo "✅ Mount verification successful"
|
||||
rm -f "$TEST_FILE"
|
||||
else
|
||||
echo "❌ Mount verification failed - mount may not be working properly"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if this is actually a FUSE mount — the plain directory created
|
||||
# above passes the file test, so without this the suite can run
|
||||
# against the local disk and report a pass it never earned
|
||||
if mount | grep -q "$SEAWEEDFS_MOUNT_POINT"; then
|
||||
echo "✅ FUSE mount detected:"
|
||||
mount | grep "$SEAWEEDFS_MOUNT_POINT"
|
||||
else
|
||||
echo "❌ Mount point not found in mount table - refusing to test a local directory"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: Run POSIX compliance tests
|
||||
id: posix-tests
|
||||
env:
|
||||
SEAWEEDFS_MOUNT_POINT: ${{ env.SEAWEEDFS_MOUNT_POINT }}
|
||||
POSIX_REQUIRE_EXTERNAL_TOOLS: ${{ env.POSIX_REQUIRE_EXTERNAL_TOOLS }}
|
||||
run: |
|
||||
cd test/fuse_integration
|
||||
|
||||
# Determine which tests to run
|
||||
TEST_TYPE="${{ github.event.inputs.test_type }}"
|
||||
if [ -z "$TEST_TYPE" ]; then
|
||||
TEST_TYPE="full"
|
||||
fi
|
||||
|
||||
echo "Running POSIX tests: $TEST_TYPE"
|
||||
if [ "$TEST_TYPE" = "full" ]; then
|
||||
echo "🚀 Running COMPREHENSIVE POSIX test suite (including external test suites)"
|
||||
else
|
||||
echo "Using mount point: $SEAWEEDFS_MOUNT_POINT"
|
||||
fi
|
||||
|
||||
# Set test configuration to use our mounted filesystem
|
||||
export TEST_MOUNT_POINT="$SEAWEEDFS_MOUNT_POINT"
|
||||
export TEST_SKIP_CLUSTER_SETUP="true"
|
||||
|
||||
# Debug: Show environment variables
|
||||
echo "🔍 Test Environment:"
|
||||
echo " TEST_MOUNT_POINT=$TEST_MOUNT_POINT"
|
||||
echo " TEST_SKIP_CLUSTER_SETUP=$TEST_SKIP_CLUSTER_SETUP"
|
||||
echo " SEAWEEDFS_MOUNT_POINT=$SEAWEEDFS_MOUNT_POINT"
|
||||
|
||||
case "$TEST_TYPE" in
|
||||
"critical")
|
||||
echo "Running critical POSIX tests (basic compliance only)..."
|
||||
make -f posix_Makefile test-posix-critical
|
||||
;;
|
||||
"basic")
|
||||
echo "Running basic POSIX tests (core file operations)..."
|
||||
make -f posix_Makefile test-posix-basic
|
||||
;;
|
||||
"extended")
|
||||
echo "Running extended POSIX tests (including advanced features)..."
|
||||
make -f posix_Makefile test-posix-extended
|
||||
;;
|
||||
"full")
|
||||
echo "🚀 Running COMPREHENSIVE POSIX test suite (all tests including external)..."
|
||||
echo " This includes: basic, extended, and external test suites"
|
||||
echo " External suites: pjdfstest, nfstest_posix, and custom stress tests"
|
||||
make -f posix_Makefile test-posix-full
|
||||
;;
|
||||
*)
|
||||
echo "Unknown test type: $TEST_TYPE, defaulting to full suite"
|
||||
echo "🚀 Running full POSIX test suite (all tests including external)..."
|
||||
make -f posix_Makefile test-posix-full
|
||||
;;
|
||||
esac
|
||||
|
||||
- name: Run external test suites
|
||||
if: ${{ github.event.inputs.enable_external_tests == 'true' }}
|
||||
continue-on-error: true
|
||||
run: |
|
||||
cd test/fuse_integration
|
||||
|
||||
# Point the external suites at the same mount as the Go tests;
|
||||
# without these the Makefile falls back to a local-disk path.
|
||||
export TEST_MOUNT_POINT="$SEAWEEDFS_MOUNT_POINT"
|
||||
export TEST_SKIP_CLUSTER_SETUP="true"
|
||||
|
||||
# Run external tests (may fail on some systems)
|
||||
make -f posix_Makefile test-nfstest-posix || echo "nfstest failed or not available"
|
||||
make -f posix_Makefile test-fio-posix || echo "FIO tests failed or not available"
|
||||
|
||||
- name: Generate compliance report
|
||||
if: always()
|
||||
run: |
|
||||
cd test/fuse_integration
|
||||
make -f posix_Makefile generate-report
|
||||
|
||||
- name: Collect system information for debugging
|
||||
if: failure()
|
||||
run: |
|
||||
cd test/fuse_integration
|
||||
mkdir -p reports/debug
|
||||
|
||||
echo "System Information" > reports/debug/system_info.txt
|
||||
uname -a >> reports/debug/system_info.txt
|
||||
echo "" >> reports/debug/system_info.txt
|
||||
|
||||
echo "Mount Information" >> reports/debug/system_info.txt
|
||||
mount >> reports/debug/system_info.txt
|
||||
echo "" >> reports/debug/system_info.txt
|
||||
|
||||
echo "Disk Space" >> reports/debug/system_info.txt
|
||||
df -h >> reports/debug/system_info.txt
|
||||
echo "" >> reports/debug/system_info.txt
|
||||
|
||||
echo "Memory Information" >> reports/debug/system_info.txt
|
||||
free -h >> reports/debug/system_info.txt
|
||||
echo "" >> reports/debug/system_info.txt
|
||||
|
||||
echo "FUSE Information" >> reports/debug/system_info.txt
|
||||
ls -la /dev/fuse >> reports/debug/system_info.txt
|
||||
lsmod | grep fuse >> reports/debug/system_info.txt
|
||||
echo "" >> reports/debug/system_info.txt
|
||||
|
||||
echo "SeaweedFS Version" >> reports/debug/system_info.txt
|
||||
weed version >> reports/debug/system_info.txt
|
||||
echo "" >> reports/debug/system_info.txt
|
||||
|
||||
echo "Go Version" >> reports/debug/system_info.txt
|
||||
go version >> reports/debug/system_info.txt
|
||||
echo "" >> reports/debug/system_info.txt
|
||||
|
||||
echo "Running Processes" >> reports/debug/system_info.txt
|
||||
ps aux | grep -E "(weed|fuse)" >> reports/debug/system_info.txt
|
||||
echo "" >> reports/debug/system_info.txt
|
||||
|
||||
# Collect SeaweedFS service logs
|
||||
echo "=== SeaweedFS Service Logs ===" >> reports/debug/system_info.txt
|
||||
if [ -f /tmp/seaweedfs/master.log ]; then
|
||||
echo "Master Log:" >> reports/debug/system_info.txt
|
||||
tail -50 /tmp/seaweedfs/master.log >> reports/debug/system_info.txt
|
||||
echo "" >> reports/debug/system_info.txt
|
||||
fi
|
||||
|
||||
if [ -f /tmp/seaweedfs/volume.log ]; then
|
||||
echo "Volume Log:" >> reports/debug/system_info.txt
|
||||
tail -50 /tmp/seaweedfs/volume.log >> reports/debug/system_info.txt
|
||||
echo "" >> reports/debug/system_info.txt
|
||||
fi
|
||||
|
||||
if [ -f /tmp/seaweedfs/filer.log ]; then
|
||||
echo "Filer Log:" >> reports/debug/system_info.txt
|
||||
tail -50 /tmp/seaweedfs/filer.log >> reports/debug/system_info.txt
|
||||
echo "" >> reports/debug/system_info.txt
|
||||
fi
|
||||
|
||||
if [ -f /tmp/seaweedfs/mount.log ]; then
|
||||
echo "Mount Log:" >> reports/debug/system_info.txt
|
||||
tail -50 /tmp/seaweedfs/mount.log >> reports/debug/system_info.txt
|
||||
echo "" >> reports/debug/system_info.txt
|
||||
fi
|
||||
|
||||
# Copy full logs as separate files
|
||||
if [ -f /tmp/seaweedfs/master.log ]; then
|
||||
cp /tmp/seaweedfs/master.log reports/debug/master.log
|
||||
fi
|
||||
if [ -f /tmp/seaweedfs/volume.log ]; then
|
||||
cp /tmp/seaweedfs/volume.log reports/debug/volume.log
|
||||
fi
|
||||
if [ -f /tmp/seaweedfs/filer.log ]; then
|
||||
cp /tmp/seaweedfs/filer.log reports/debug/filer.log
|
||||
fi
|
||||
if [ -f /tmp/seaweedfs/mount.log ]; then
|
||||
cp /tmp/seaweedfs/mount.log reports/debug/mount.log
|
||||
fi
|
||||
|
||||
- name: Cleanup SeaweedFS cluster and FUSE mount
|
||||
if: always()
|
||||
run: |
|
||||
stop_process() {
|
||||
local name=$1
|
||||
local pidfile=$2
|
||||
if [ -f "$pidfile" ]; then
|
||||
local pid=$(cat "$pidfile")
|
||||
if kill -0 $pid 2>/dev/null; then
|
||||
echo "Stopping $name process (PID: $pid)..."
|
||||
kill -TERM $pid || true
|
||||
sleep 2
|
||||
kill -KILL $pid 2>/dev/null || true
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
echo "Cleaning up SeaweedFS cluster and FUSE mount..."
|
||||
|
||||
MOUNT_POINT="/tmp/seaweedfs/mount"
|
||||
if mountpoint -q $MOUNT_POINT 2>/dev/null; then
|
||||
echo "Unmounting FUSE filesystem..."
|
||||
fusermount -u $MOUNT_POINT || umount $MOUNT_POINT || true
|
||||
fi
|
||||
|
||||
stop_process "mount" "/tmp/seaweedfs/mount.pid"
|
||||
stop_process "filer" "/tmp/seaweedfs/filer.pid"
|
||||
stop_process "volume" "/tmp/seaweedfs/volume.pid"
|
||||
stop_process "master" "/tmp/seaweedfs/master.pid"
|
||||
|
||||
pkill -f "weed " || true
|
||||
|
||||
fusermount -u $MOUNT_POINT 2>/dev/null || true
|
||||
umount $MOUNT_POINT 2>/dev/null || true
|
||||
|
||||
rm -rf /tmp/seaweedfs || true
|
||||
|
||||
echo "Cleanup completed"
|
||||
|
||||
|
||||
- name: Upload test results
|
||||
uses: actions/upload-artifact@v7
|
||||
if: always()
|
||||
with:
|
||||
name: posix-test-results-ubuntu-fuse${{ matrix.fuse-version }}
|
||||
path: |
|
||||
test/fuse_integration/reports/
|
||||
test/fuse_integration/*.log
|
||||
retention-days: 30
|
||||
|
||||
- name: Upload test coverage
|
||||
uses: actions/upload-artifact@v7
|
||||
if: always() && (github.event.inputs.test_type == 'full' || github.event.inputs.test_type == 'extended')
|
||||
with:
|
||||
name: posix-coverage-ubuntu
|
||||
path: test/fuse_integration/reports/posix_coverage.html
|
||||
retention-days: 14
|
||||
|
||||
- name: Comment PR with results
|
||||
if: github.event_name == 'pull_request' && always()
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const reportPath = 'test/fuse_integration/reports/posix_compliance_summary.txt';
|
||||
|
||||
if (fs.existsSync(reportPath)) {
|
||||
const report = fs.readFileSync(reportPath, 'utf8');
|
||||
const comment = `## POSIX Compliance Test Results
|
||||
|
||||
**Go Version:** ${{ env.GO_VERSION }}
|
||||
**FUSE Version:** ${{ matrix.fuse-version }}
|
||||
**Test Type:** ${{ github.event.inputs.test_type || 'full' }}
|
||||
|
||||
<details>
|
||||
<summary>Test Summary</summary>
|
||||
|
||||
\`\`\`
|
||||
${report}
|
||||
\`\`\`
|
||||
</details>
|
||||
|
||||
📊 Full results available in [test artifacts](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
|
||||
`;
|
||||
|
||||
try {
|
||||
await github.rest.issues.createComment({
|
||||
issue_number: context.issue.number,
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
body: comment
|
||||
});
|
||||
} catch (err) {
|
||||
console.log('Could not post PR comment: ' + err.message);
|
||||
}
|
||||
}
|
||||
|
||||
posix-compliance-macos:
|
||||
runs-on: macos-latest
|
||||
timeout-minutes: 60
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v7
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v7
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
cache: true
|
||||
|
||||
- name: Install macFUSE
|
||||
run: |
|
||||
# Install macFUSE
|
||||
brew install macfuse
|
||||
|
||||
# Note: macFUSE may require system extension approval on macOS
|
||||
# This step may need manual intervention in some cases
|
||||
|
||||
- name: Build SeaweedFS
|
||||
run: |
|
||||
make
|
||||
# Verify binary exists and is executable (it's installed to GOPATH/bin)
|
||||
weed version
|
||||
|
||||
- name: Run critical POSIX tests (macOS)
|
||||
continue-on-error: true # macOS FUSE can be more restrictive
|
||||
run: |
|
||||
cd test/fuse_integration
|
||||
|
||||
if [ ! -f go.mod ]; then
|
||||
go mod init seaweedfs-posix-tests
|
||||
go mod tidy
|
||||
fi
|
||||
|
||||
mkdir -p reports
|
||||
|
||||
# Run basic tests only on macOS due to FUSE limitations
|
||||
make -f posix_Makefile test-posix-critical
|
||||
|
||||
- name: Upload macOS test results
|
||||
uses: actions/upload-artifact@v7
|
||||
if: always()
|
||||
with:
|
||||
name: posix-test-results-macos
|
||||
path: |
|
||||
test/fuse_integration/reports/
|
||||
test/fuse_integration/*.log
|
||||
retention-days: 30
|
||||
|
||||
security-analysis:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name == 'pull_request'
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v7
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v7
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
|
||||
- name: Install security analysis tools
|
||||
run: |
|
||||
echo "Installing staticcheck for security analysis..."
|
||||
go install honnef.co/go/tools/cmd/staticcheck@latest
|
||||
|
||||
- name: Run security analysis on FUSE code
|
||||
run: |
|
||||
# Analyze mount and FUSE-related code for security and quality issues
|
||||
echo "Running staticcheck security analysis..."
|
||||
|
||||
# Run staticcheck on FUSE-related code
|
||||
echo "Analyzing FUSE mount code..."
|
||||
staticcheck ./weed/mount/... > staticcheck-report.txt 2>&1 || true
|
||||
staticcheck ./weed/command/mount* ./weed/command/fuse* >> staticcheck-report.txt 2>&1 || true
|
||||
|
||||
# Convert to JSON format for consistency
|
||||
echo "{" > gosec-report.json
|
||||
echo " \"tool\": \"staticcheck\"," >> gosec-report.json
|
||||
echo " \"timestamp\": \"$(date -Iseconds)\"," >> gosec-report.json
|
||||
echo " \"issues\": [" >> gosec-report.json
|
||||
|
||||
if [ -s staticcheck-report.txt ]; then
|
||||
echo " \"$(cat staticcheck-report.txt | head -20 | sed 's/"/\\"/g' | tr '\n' ' ')\"" >> gosec-report.json
|
||||
fi
|
||||
|
||||
echo " ]," >> gosec-report.json
|
||||
echo " \"stats\": {" >> gosec-report.json
|
||||
echo " \"files_analyzed\": $(find ./weed/mount ./weed/command -name '*.go' | wc -l)," >> gosec-report.json
|
||||
echo " \"issues_found\": $(wc -l < staticcheck-report.txt 2>/dev/null || echo 0)" >> gosec-report.json
|
||||
echo " }" >> gosec-report.json
|
||||
echo "}" >> gosec-report.json
|
||||
|
||||
echo "Security analysis completed"
|
||||
echo "Issues found: $(wc -l < staticcheck-report.txt 2>/dev/null || echo 0)"
|
||||
|
||||
- name: Upload security analysis results
|
||||
uses: actions/upload-artifact@v7
|
||||
if: always()
|
||||
with:
|
||||
name: security-analysis-results
|
||||
path: gosec-report.json
|
||||
retention-days: 30
|
||||
|
||||
performance-baseline:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event.inputs.test_type == 'full' || github.event_name == 'schedule'
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v7
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v7
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y fuse libfuse-dev fio
|
||||
sudo usermod -a -G fuse $USER
|
||||
sudo chmod 666 /dev/fuse
|
||||
|
||||
- name: Build SeaweedFS
|
||||
run: make
|
||||
|
||||
- name: Run performance baseline tests
|
||||
run: |
|
||||
cd test/fuse_integration
|
||||
|
||||
if [ ! -f go.mod ]; then
|
||||
go mod init seaweedfs-posix-tests
|
||||
go mod tidy
|
||||
fi
|
||||
|
||||
mkdir -p reports
|
||||
|
||||
# Run performance benchmarks
|
||||
make -f posix_Makefile benchmark-posix
|
||||
# This job has no FUSE mount, so the fio suite can only skip; keep the
|
||||
# run green but honest about producing no mounted-FS results
|
||||
make -f posix_Makefile test-fio-posix || echo "FIO skipped - no mounted filesystem in this job"
|
||||
|
||||
- name: Store performance baseline
|
||||
uses: actions/upload-artifact@v7
|
||||
with:
|
||||
name: performance-baseline-results
|
||||
path: |
|
||||
test/fuse_integration/reports/posix_benchmark_results.log
|
||||
test/fuse_integration/reports/fio_*_results.log
|
||||
retention-days: 90
|
||||
|
||||
# Schedule regular compliance checks
|
||||
scheduled-compliance-check:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name == 'schedule'
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v7
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v7
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y fuse libfuse-dev
|
||||
pip3 install --user nfstest
|
||||
|
||||
- name: Build SeaweedFS
|
||||
run: make
|
||||
|
||||
- name: Run comprehensive compliance check
|
||||
run: |
|
||||
cd test/fuse_integration
|
||||
make -f posix_Makefile test-posix-full
|
||||
make -f posix_Makefile generate-report
|
||||
|
||||
- name: Create compliance issue if tests fail
|
||||
if: failure()
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
const issue = await github.rest.issues.create({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
title: 'POSIX Compliance Check Failed - ' + new Date().toISOString().split('T')[0],
|
||||
body: `## POSIX Compliance Check Failure
|
||||
|
||||
The scheduled POSIX compliance check has failed. This may indicate:
|
||||
|
||||
- Regression in FUSE mount functionality
|
||||
- Changes affecting POSIX compatibility
|
||||
- Infrastructure issues with the test environment
|
||||
|
||||
**Action Required:** Review the test results and investigate any failures.
|
||||
|
||||
**Test Run:** [Workflow Run](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId})
|
||||
|
||||
**Date:** ${new Date().toISOString()}
|
||||
|
||||
---
|
||||
This issue was automatically created by the POSIX compliance workflow.
|
||||
`,
|
||||
labels: ['bug', 'posix-compliance', 'automated-issue']
|
||||
});
|
||||
|
||||
console.log('Created issue:', issue.data.number);
|
||||
|
||||
# Schedule this workflow to run weekly
|
||||
# Uncomment the following lines to enable scheduled runs:
|
||||
# schedule:
|
||||
# - cron: '0 2 * * 1' # Every Monday at 2 AM UTC
|
||||
|
||||
@@ -225,7 +225,7 @@ The full operation list is in [Amazon S3 API][AmazonS3API], and [Supported APIs
|
||||
|
||||
SeaweedFS is a lakehouse in one system. [S3 Table Buckets][S3TableBucket] hold Apache Iceberg tables by default, or [Lance][LanceCatalog] tables for vectors and multimodal data, and the built-in [Iceberg REST Catalog][IcebergCatalog] and Lance namespace serve them directly. There is no Hive Metastore, Glue, or separate catalog service to deploy, secure, and back up.
|
||||
|
||||
* Query engines operate on the same tables at the same time: [Spark][SparkIceberg], [Trino][TrinoIceberg], [Dremio][DremioIceberg], [DuckDB][DuckDBIceberg], [Apache Doris][DorisIceberg], [RisingWave][RisingWaveIceberg], ClickHouse, and [LanceDB][LanceDB]. Catalog commits are atomic compare-and-swap, so concurrent writers are safe. [Lakekeeper][Lakekeeper] can front the same storage with STS-vended credentials. [OLake][OLake] replicates PostgreSQL, MySQL, and MongoDB into the same tables with change data capture.
|
||||
* Query engines operate on the same tables at the same time: [Spark][SparkIceberg], [Trino][TrinoIceberg], [Dremio][DremioIceberg], [DuckDB][DuckDBIceberg], [Apache Doris][DorisIceberg], [RisingWave][RisingWaveIceberg], ClickHouse, and [LanceDB][LanceDB]. Catalog commits are atomic compare-and-swap, so concurrent writers are safe. [Lakekeeper][Lakekeeper] can front the same storage with STS-vended credentials. [OLake Go][OLake] is a high-performance, open-source EL (Extract–Load) platform that bridges operational databases and open lakehouse storage - writing Apache Iceberg tables or Parquet files on Amazon S3.
|
||||
* [Automated table maintenance][IcebergMaintenance]: compaction, snapshot expiration, orphan file removal, and manifest rewriting, configured per bucket or table through the S3 Tables maintenance APIs, and the same for [Lance][LanceMaintenance].
|
||||
* IAM at the bucket, namespace, and table level with standard bucket policies, see [S3 Tables Security][S3TablesSecurity].
|
||||
* A [Hadoop compatible file system][Hadoop] for Spark, Flink, and HBase.
|
||||
@@ -487,7 +487,7 @@ Your support will be really appreciated by me and other supporters!
|
||||
[RisingWaveIceberg]: https://github.com/seaweedfs/seaweedfs/wiki/RisingWave-Iceberg-Integration
|
||||
[LanceDB]: https://github.com/seaweedfs/seaweedfs/wiki/LanceDB-Integration
|
||||
[Lakekeeper]: https://github.com/seaweedfs/seaweedfs/wiki/Lakekeeper-Iceberg-Integration
|
||||
[OLake]: https://github.com/seaweedfs/seaweedfs/wiki/OLake-Iceberg-Integration
|
||||
[OLake]: https://github.com/seaweedfs/seaweedfs/wiki/OLake-Go-Iceberg-Integration
|
||||
[IcebergMaintenance]: https://github.com/seaweedfs/seaweedfs/wiki/Iceberg-Table-Maintenance
|
||||
[LanceMaintenance]: https://github.com/seaweedfs/seaweedfs/wiki/Lance-Maintenance-Worker
|
||||
[S3TablesSecurity]: https://github.com/seaweedfs/seaweedfs/wiki/S3-Tables-Security
|
||||
|
||||
+1099
-1050
File diff suppressed because it is too large.
Load diff
|
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 55 KiB |
Generated
+40
-15
@@ -86,7 +86,7 @@ version = "1.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
|
||||
dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.60.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -97,7 +97,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"once_cell_polyfill",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.60.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -510,10 +510,12 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "aws-smithy-json"
|
||||
version = "0.62.5"
|
||||
version = "0.62.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9648b0bb82a2eedd844052c6ad2a1a822d1f8e3adee5fbf668366717e428856a"
|
||||
checksum = "701a947f4797e52a911e114a898667c746c39feea467bbd1abd7b3721f702ffa"
|
||||
dependencies = [
|
||||
"aws-smithy-runtime-api",
|
||||
"aws-smithy-schema",
|
||||
"aws-smithy-types",
|
||||
]
|
||||
|
||||
@@ -563,11 +565,12 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "aws-smithy-runtime-api"
|
||||
version = "1.11.6"
|
||||
version = "1.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "876ab3c9c29791ba4ba02b780a3049e21ec63dabda09268b175272c3733a79e6"
|
||||
checksum = "9db177daa6ba8afb9ee1aefcf548c907abcf52065e394ee11a92780057fe0e8c"
|
||||
dependencies = [
|
||||
"aws-smithy-async",
|
||||
"aws-smithy-runtime-api-macros",
|
||||
"aws-smithy-types",
|
||||
"bytes",
|
||||
"http 0.2.12",
|
||||
@@ -579,10 +582,32 @@ dependencies = [
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aws-smithy-types"
|
||||
version = "1.4.7"
|
||||
name = "aws-smithy-runtime-api-macros"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9d73dbfbaa8e4bc57b9045137680b958d274823509a360abfd8e1d514d40c95c"
|
||||
checksum = "8d7396fd9500589e62e460e987ecb671bad374934e55ec3b5f498cc7a8a8a7b7"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aws-smithy-schema"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7442cb268338f0eb8278140a107c046756aa01093d8ef5e99628d34ae09c94f5"
|
||||
dependencies = [
|
||||
"aws-smithy-runtime-api",
|
||||
"aws-smithy-types",
|
||||
"http 1.4.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aws-smithy-types"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32b42fcf341259d85ca10fac9a2f6448a8ec691c6955a18e45bc3b71a85fab85"
|
||||
dependencies = [
|
||||
"base64-simd",
|
||||
"bytes",
|
||||
@@ -1325,7 +1350,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2379,7 +2404,7 @@ version = "0.50.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
|
||||
dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3391,7 +3416,7 @@ dependencies = [
|
||||
"errno 0.3.14",
|
||||
"libc",
|
||||
"linux-raw-sys 0.12.1",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3676,7 +3701,7 @@ version = "1.4.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b"
|
||||
dependencies = [
|
||||
"errno 0.3.14",
|
||||
"errno 0.2.8",
|
||||
"libc",
|
||||
]
|
||||
|
||||
@@ -3743,7 +3768,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.60.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3877,7 +3902,7 @@ dependencies = [
|
||||
"getrandom 0.4.2",
|
||||
"once_cell",
|
||||
"rustix 1.1.4",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -16,7 +16,7 @@ use crate::pb::filer_pb;
|
||||
use crate::pb::master_pb;
|
||||
use crate::pb::volume_server_pb;
|
||||
use crate::pb::volume_server_pb::volume_server_server::VolumeServer;
|
||||
use crate::storage::erasure_coding::ec_shard::{DATA_SHARDS_COUNT, ShardId, shard_id_try_from};
|
||||
use crate::storage::erasure_coding::ec_shard::{ShardId, shard_id_try_from};
|
||||
use crate::storage::erasure_coding::ecj_merge::EcjIdDecoder;
|
||||
use crate::storage::needle::needle::{self, Needle};
|
||||
use crate::storage::types::*;
|
||||
@@ -53,6 +53,12 @@ fn needle_not_found(needle_id: NeedleId) -> Status {
|
||||
Status::not_found(format!("needle not found {}", needle_id.0))
|
||||
}
|
||||
|
||||
/// Mirrors Go's `isNotLeaderErr`: the master returns raft's NotLeaderError,
|
||||
/// which arrives as an Unknown-status message.
|
||||
fn is_not_leader_status(e: &Status) -> bool {
|
||||
e.message().contains("Not current leader")
|
||||
}
|
||||
|
||||
/// Deadline from the caller's `grpc-timeout` header (TimeoutValue +
|
||||
/// H|M|S|m|u|n unit, per the gRPC-over-HTTP2 spec). Tonic's GrpcTimeout layer
|
||||
/// drops an over-deadline handler future anyway; parsing it here lets a queued
|
||||
@@ -344,22 +350,46 @@ impl VolumeGrpcService {
|
||||
is_readonly: bool,
|
||||
) -> Result<(), Status> {
|
||||
// VolumeMarkReadonly mutates raft-replicated master topology, so it must
|
||||
// reach the leader. Prefer the live leader the heartbeat is talking to
|
||||
// (current_master_url), falling back to the static seed before the first
|
||||
// heartbeat — mirrors store_ec.rs and Go's vs.GetMaster(). Sending it to
|
||||
// the static seed fails "not current leader" after any master failover.
|
||||
let master_url = {
|
||||
let live = self.state.current_master_url.read().await.clone();
|
||||
if !live.is_empty() {
|
||||
live
|
||||
} else {
|
||||
self.state.master_url.clone()
|
||||
}
|
||||
};
|
||||
// reach the leader the heartbeat confirmed. On "Not current leader",
|
||||
// ask the seed masters who leads and retry there — mirrors Go's
|
||||
// notifyMasterVolumeReadonly.
|
||||
let master_url = self.state.current_master().await;
|
||||
if master_url.is_empty() {
|
||||
return Ok(());
|
||||
if !self.state.has_master {
|
||||
return Ok(());
|
||||
}
|
||||
return Err(Status::unavailable("no master leader known"));
|
||||
}
|
||||
let grpc_addr = parse_grpc_address(&master_url).map_err(|e| {
|
||||
match self
|
||||
.volume_mark_readonly_on_master(&master_url, info, is_readonly)
|
||||
.await
|
||||
{
|
||||
Err(e) if is_not_leader_status(&e) => {
|
||||
let leader = super::heartbeat::lookup_leader_from_peers(
|
||||
&self.state.master_urls,
|
||||
self.state.outgoing_grpc_tls.as_ref(),
|
||||
)
|
||||
.await
|
||||
.ok_or_else(|| {
|
||||
Status::unavailable(format!(
|
||||
"master {} rejected mark-readonly and no peer named a leader",
|
||||
master_url
|
||||
))
|
||||
})?;
|
||||
self.volume_mark_readonly_on_master(&leader, info, is_readonly)
|
||||
.await
|
||||
}
|
||||
result => result,
|
||||
}
|
||||
}
|
||||
|
||||
async fn volume_mark_readonly_on_master(
|
||||
&self,
|
||||
master_url: &str,
|
||||
info: &MasterVolumeInfo,
|
||||
is_readonly: bool,
|
||||
) -> Result<(), Status> {
|
||||
let grpc_addr = parse_grpc_address(master_url).map_err(|e| {
|
||||
Status::internal(format!("invalid master address {}: {}", master_url, e))
|
||||
})?;
|
||||
let channel = connect_channel(
|
||||
@@ -1353,6 +1383,7 @@ impl VolumeServer for VolumeGrpcService {
|
||||
// Go's CommitCompact waits out leased readers under
|
||||
// dataFileAccessLock — do it without holding the store
|
||||
// guard, so a slow download stalls only this vacuum.
|
||||
let _sync_guard = vol.data_file_sync_lock().lock().unwrap();
|
||||
let _lease = vol.instance().write_lock();
|
||||
let is_read_only = vol.is_read_only();
|
||||
vol.commit_compact_held()
|
||||
@@ -2317,9 +2348,9 @@ impl VolumeServer for VolumeGrpcService {
|
||||
// owns the .ecx on disk (volume not yet mounted — relevant
|
||||
// when shards stream in mid-rebuild before any
|
||||
// VolumeEcShardsMount has happened; see #9212), then any
|
||||
// HDD, then any disk. Pass the build's default data-shard
|
||||
// count for free-slot maths; the helper takes it as a
|
||||
// parameter so custom-ratio builds can swap it.
|
||||
// HDD, then any disk. The free-slot maths uses the
|
||||
// volume's own data-shard count from its .vif so a
|
||||
// custom-ratio volume is not scored as 10+4.
|
||||
let vid = VolumeId(info.volume_id);
|
||||
let dir = if info.disk_id > 0 {
|
||||
let count = store.locations.len();
|
||||
@@ -2334,11 +2365,13 @@ impl VolumeServer for VolumeGrpcService {
|
||||
} else {
|
||||
// The mounted-volume refusal above means no disk
|
||||
// holds an in-memory claim here.
|
||||
let data_shards =
|
||||
store.lookup_ec_data_shards(&info.collection, vid);
|
||||
store
|
||||
.find_ec_shard_target_location(
|
||||
&info.collection,
|
||||
vid,
|
||||
DATA_SHARDS_COUNT as u32,
|
||||
data_shards,
|
||||
&[],
|
||||
)
|
||||
.map(|i| store.locations[i].directory.clone())
|
||||
@@ -3288,9 +3321,9 @@ impl VolumeServer for VolumeGrpcService {
|
||||
// volume mounted, then a disk that owns the .ecx on disk (volume
|
||||
// not yet mounted — relevant for ec.rebuild, where only the
|
||||
// first shard carries .ecx and subsequent shards must land on
|
||||
// the same disk; see #9212), then any HDD, then any disk. Pass
|
||||
// the build's default data-shard count; the helper takes it as a
|
||||
// parameter so custom-ratio builds can swap it.
|
||||
// the same disk; see #9212), then any HDD, then any disk. The
|
||||
// free-slot maths uses the volume's own data-shard count from its
|
||||
// .vif so a custom-ratio volume is not scored as 10+4.
|
||||
let (dest_dir, dest_idx_dir) = {
|
||||
let store = self.state.store.read().unwrap();
|
||||
let count = store.locations.len();
|
||||
@@ -3322,10 +3355,11 @@ impl VolumeServer for VolumeGrpcService {
|
||||
req.volume_id, shard_ids, dirs
|
||||
)));
|
||||
}
|
||||
let data_shards = store.lookup_ec_data_shards(&req.collection, vid);
|
||||
match store.find_ec_shard_target_location(
|
||||
&req.collection,
|
||||
vid,
|
||||
DATA_SHARDS_COUNT as u32,
|
||||
data_shards,
|
||||
&shard_ids,
|
||||
) {
|
||||
Some(i) => {
|
||||
@@ -4723,6 +4757,7 @@ impl VolumeServer for VolumeGrpcService {
|
||||
// backend swap: between clearing the remote-only write
|
||||
// restriction and attaching the local .dat a write would
|
||||
// pass check_writable and then fail on a missing dat_file.
|
||||
let _sync_guard = vol.data_file_sync_lock().lock().unwrap();
|
||||
let _lease = vol.instance().write_lock();
|
||||
|
||||
// The store guard dropped before the lease was taken, so an
|
||||
@@ -5771,7 +5806,8 @@ impl VolumeCopyJob {
|
||||
(&self.dest.data_base_name, self.dest.has_remote_dat);
|
||||
let mut preallocate_size: i64 = 0;
|
||||
if !has_remote_dat {
|
||||
let grpc_addr = super::heartbeat::to_grpc_address(&state.master_url);
|
||||
let master = state.current_master().await;
|
||||
let grpc_addr = super::heartbeat::to_grpc_address(&master);
|
||||
// Race the master-configuration RPC against the caller's
|
||||
// response channel: a stalled master (or a slow leader
|
||||
// election) would otherwise hold the task and its .note
|
||||
@@ -5796,7 +5832,7 @@ impl VolumeCopyJob {
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!("get master {} configuration: {}", state.master_url, e);
|
||||
tracing::warn!("get master {} configuration: {}", master, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -603,7 +603,7 @@ async fn batch_delete_file_ids(
|
||||
let locations = lookup_volume(
|
||||
&state.http_client,
|
||||
&state.outgoing_http_scheme,
|
||||
&state.master_url,
|
||||
&state.current_master().await,
|
||||
volume_id,
|
||||
)
|
||||
.await
|
||||
@@ -675,7 +675,7 @@ async fn do_replicated_request(
|
||||
let locations = lookup_volume(
|
||||
&state.http_client,
|
||||
&state.outgoing_http_scheme,
|
||||
&state.master_url,
|
||||
&state.current_master().await,
|
||||
vid,
|
||||
)
|
||||
.await
|
||||
@@ -832,7 +832,7 @@ async fn proxy_or_redirect_to_target(
|
||||
let locations = match lookup_volume(
|
||||
&state.http_client,
|
||||
&state.outgoing_http_scheme,
|
||||
&state.master_url,
|
||||
&state.current_master().await,
|
||||
vid.0,
|
||||
)
|
||||
.await
|
||||
@@ -1238,7 +1238,7 @@ async fn proxy_missing_volume(
|
||||
let query_string = uri.query().unwrap_or("").to_string();
|
||||
if is_proxied_query(&query_string)
|
||||
|| state.read_mode == ReadMode::Local
|
||||
|| state.master_url.is_empty()
|
||||
|| state.current_master().await.is_empty()
|
||||
{
|
||||
return StatusCode::NOT_FOUND.into_response();
|
||||
}
|
||||
@@ -1282,7 +1282,7 @@ async fn check_download_limit(
|
||||
// Go tries proxy to replica once before the wait loop
|
||||
// (checkDownloadLimit); it does not retry on each wakeup.
|
||||
let should_try_replica =
|
||||
!is_proxied_query(&query_string) && !state.master_url.is_empty() && {
|
||||
!is_proxied_query(&query_string) && !state.current_master().await.is_empty() && {
|
||||
let store = state.store.read().unwrap();
|
||||
store
|
||||
.find_volume(vid)
|
||||
@@ -2960,7 +2960,9 @@ pub async fn post_handler(
|
||||
|
||||
// Replicate to remote volume servers if this volume has replicas.
|
||||
// Matches Go's GetWritableRemoteReplications: skip if copy_count == 1.
|
||||
if !is_replicate && write_result.is_ok() && !state.master_url.is_empty() {
|
||||
// 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
|
||||
@@ -3358,7 +3360,7 @@ pub async fn delete_handler(
|
||||
};
|
||||
|
||||
let is_replicate = del_query.split('&').any(|p| p == "type=replicate");
|
||||
if !is_replicate && delete_result.is_ok() && !state.master_url.is_empty() {
|
||||
if !is_replicate && delete_result.is_ok() {
|
||||
let needs_replication = {
|
||||
let store = state.store.read().unwrap();
|
||||
store
|
||||
@@ -4232,7 +4234,7 @@ async fn read_remote_chunk_needle(
|
||||
let locations = lookup_volume(
|
||||
&state.http_client,
|
||||
&state.outgoing_http_scheme,
|
||||
&state.master_url,
|
||||
&state.current_master().await,
|
||||
vid.0,
|
||||
)
|
||||
.await?;
|
||||
@@ -6671,7 +6673,8 @@ mod tests {
|
||||
let s = Arc::get_mut(&mut state).unwrap();
|
||||
// Empty self_url makes `url.contains(self_url)` true for every peer.
|
||||
s.self_url = "198.51.100.8:8080".to_string();
|
||||
s.master_url = master_url;
|
||||
s.master_url = master_url.clone();
|
||||
s.current_master_url = tokio::sync::RwLock::new(master_url);
|
||||
state
|
||||
}
|
||||
|
||||
|
||||
@@ -155,7 +155,11 @@ pub async fn run_heartbeat_with_state(
|
||||
}
|
||||
PostAction::SleepPulse => {
|
||||
new_leader = None;
|
||||
tokio::time::sleep(pulse).await;
|
||||
// The lookup spends part of the retry delay rather than
|
||||
// adding to it.
|
||||
let retry_at = tokio::time::Instant::now() + pulse;
|
||||
refresh_leader_from_peers(&config, &state, pulse).await;
|
||||
tokio::time::sleep_until(retry_at).await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,10 +258,56 @@ pub async fn try_get_master_configuration(
|
||||
Ok(resp.into_inner())
|
||||
}
|
||||
|
||||
/// Publish the leader the configured masters report, so lookups stop going
|
||||
/// to the master the heartbeat just lost while the loop retries. Without an
|
||||
/// answer inside `timeout` the published master is cleared: the lost master
|
||||
/// is dead or no longer leader, and a follower answers lookups from its own
|
||||
/// cache rather than refusing them. Mirrors Go's refreshLeaderFromPeers.
|
||||
async fn refresh_leader_from_peers(
|
||||
config: &HeartbeatConfig,
|
||||
state: &Arc<VolumeServerState>,
|
||||
timeout: Duration,
|
||||
) {
|
||||
let lookup = lookup_leader_from_peers(
|
||||
&config.master_addresses,
|
||||
state.outgoing_grpc_tls.as_ref(),
|
||||
);
|
||||
match tokio::time::timeout(timeout, lookup).await {
|
||||
Ok(Some(leader)) => publish_current_master(state, &leader).await,
|
||||
_ => state.current_master_url.write().await.clear(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Ask each seed master for the raft leader and return the first answer, or
|
||||
/// None when none replies with one. Mirrors Go's
|
||||
/// `operation.LookupRaftLeaderMaster`.
|
||||
pub async fn lookup_leader_from_peers(
|
||||
master_addrs: &[String],
|
||||
tls: Option<&super::grpc_client::OutgoingGrpcTlsConfig>,
|
||||
) -> Option<String> {
|
||||
for master_addr in master_addrs {
|
||||
let grpc_addr = to_grpc_address(master_addr);
|
||||
if let Ok(resp) = try_get_master_configuration(&grpc_addr, tls).await
|
||||
&& !resp.leader.is_empty()
|
||||
{
|
||||
return Some(resp.leader);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn is_stopping(state: &VolumeServerState) -> bool {
|
||||
*state.is_stopping.read().unwrap()
|
||||
}
|
||||
|
||||
/// Store the master lookups should go to, in the address form it was
|
||||
/// announced in — `host:port` or `host:port.grpcPort` — so an explicit gRPC
|
||||
/// port survives to the gRPC callers, mirroring Go's setCurrentMaster.
|
||||
/// The heartbeat calls this only for a master known to lead.
|
||||
async fn publish_current_master(state: &Arc<VolumeServerState>, master: &str) {
|
||||
*state.current_master_url.write().await = master.to_string();
|
||||
}
|
||||
|
||||
fn duplicate_uuid_retry_delay(retry_count: u32) -> Duration {
|
||||
Duration::from_secs((1u64 << retry_count) * 2)
|
||||
}
|
||||
@@ -430,14 +480,6 @@ async fn do_heartbeat(
|
||||
let mut response_stream = client.send_heartbeat(stream).await?.into_inner();
|
||||
|
||||
info!("Heartbeat stream established with {}", grpc_addr);
|
||||
// Publish the master we're now talking to in canonical http host:port
|
||||
// form so Ping admission can recognise it once a leader change moves us
|
||||
// off the seed list. Mirrors Go's vs.setCurrentMaster(masterAddress).
|
||||
{
|
||||
let normalised = super::volume_server::to_http_address(current_master).into_owned();
|
||||
let mut guard = state.current_master_url.write().await;
|
||||
*guard = normalised;
|
||||
}
|
||||
if is_stopping(state) {
|
||||
state.is_heartbeating.store(false, Ordering::Relaxed);
|
||||
send_deregister_heartbeat(config, state, &tx).await;
|
||||
@@ -451,13 +493,31 @@ async fn do_heartbeat(
|
||||
volume_tick.tick().await;
|
||||
ec_tick.tick().await;
|
||||
|
||||
let mut confirmed_leader = false;
|
||||
loop {
|
||||
tokio::select! {
|
||||
resp = response_stream.message() => {
|
||||
match resp {
|
||||
Ok(Some(hb_resp)) => {
|
||||
// Match Go ordering: DuplicatedUuids first, then volume
|
||||
// options, then leader redirect.
|
||||
// A reply names the leader or comes from it, so it
|
||||
// settles where lookups go before anything else on the
|
||||
// reply is handled: a follower reply carries no other
|
||||
// fields, and a leader stays the lookup target even
|
||||
// when it rejects this registration. Mirrors Go.
|
||||
if !hb_resp.leader.is_empty() {
|
||||
if super::volume_server::to_http_address(current_master)
|
||||
!= super::volume_server::to_http_address(&hb_resp.leader)
|
||||
{
|
||||
publish_current_master(state, &hb_resp.leader).await;
|
||||
return Ok(Some(hb_resp.leader));
|
||||
}
|
||||
} else if !confirmed_leader {
|
||||
// The leader answers every new stream without
|
||||
// naming a leader, so this peer is the one lookups
|
||||
// go to.
|
||||
confirmed_leader = true;
|
||||
publish_current_master(state, current_master).await;
|
||||
}
|
||||
if !hb_resp.duplicated_uuids.is_empty() {
|
||||
let uuids = hb_resp.duplicated_uuids.clone();
|
||||
let duplicate_dirs = off_runtime(config, state, move |_, state| {
|
||||
@@ -502,11 +562,6 @@ async fn do_heartbeat(
|
||||
if metrics_changed {
|
||||
state.metrics_notify.notify_waiters();
|
||||
}
|
||||
// Match Go: only redirect if leader is non-empty AND
|
||||
// different from the current master we're connected to.
|
||||
if !hb_resp.leader.is_empty() && current_master != hb_resp.leader {
|
||||
return Ok(Some(hb_resp.leader));
|
||||
}
|
||||
}
|
||||
Ok(None) => return Ok(None),
|
||||
Err(e) => return Err(Box::new(e)),
|
||||
|
||||
@@ -1139,16 +1139,9 @@ async fn cached_lookup_ec_shard_locations(
|
||||
state: &Arc<VolumeServerState>,
|
||||
vid: VolumeId,
|
||||
) -> io::Result<HashMap<ShardId, Vec<String>>> {
|
||||
let master = {
|
||||
let live = state.current_master_url.read().await.clone();
|
||||
if !live.is_empty() {
|
||||
live
|
||||
} else {
|
||||
state.master_url.clone()
|
||||
}
|
||||
};
|
||||
let master = state.current_master().await;
|
||||
if master.is_empty() {
|
||||
return Err(io::Error::other("no master configured for ec shard lookup"));
|
||||
return Err(io::Error::other("no master leader known for ec shard lookup"));
|
||||
}
|
||||
|
||||
let grpc_addr =
|
||||
|
||||
@@ -86,7 +86,8 @@ pub struct VolumeServerState {
|
||||
/// If true, FetchAndWriteNeedle skips remote S3 endpoint validation,
|
||||
/// allowing arbitrary (incl. loopback / link-local / metadata) hosts.
|
||||
pub allow_untrusted_remote_endpoints: bool,
|
||||
/// First master address for volume lookups (e.g., "localhost:9333").
|
||||
/// First configured master address (e.g., "localhost:9333"). A seed, not a
|
||||
/// lookup target: lookups go to `current_master_url`.
|
||||
pub master_url: String,
|
||||
/// Seed master addresses for UI rendering.
|
||||
pub master_urls: Vec<String>,
|
||||
@@ -94,10 +95,12 @@ pub struct VolumeServerState {
|
||||
/// Built once at construction so Ping admission stays O(1). Mirrors
|
||||
/// Go's `seedMasterSet` on `VolumeServer`.
|
||||
pub seed_master_set: std::collections::HashSet<String>,
|
||||
/// Current master this server is heartbeating with, in canonical http
|
||||
/// `host:port` form. Empty when no heartbeat connection is active. The
|
||||
/// heartbeat goroutine writes; admission reads — the lock keeps them
|
||||
/// from racing on a leader change. Mirrors Go's `currentMaster` plus
|
||||
/// Master this server takes as leader, in its announced
|
||||
/// `host:port[.grpcPort]` form — callers pick http or gRPC form per use.
|
||||
/// Empty until the heartbeat confirms one — a peer that answered a
|
||||
/// stream without naming a leader, or the leader a follower named — and
|
||||
/// cleared again when no master reports one. The heartbeat writes;
|
||||
/// admission and lookups read. Mirrors Go's `currentMaster` plus
|
||||
/// `currentMasterLock`.
|
||||
pub current_master_url: tokio::sync::RwLock<String>,
|
||||
/// This server's own address (ip:port) for filtering self from lookup results.
|
||||
@@ -140,6 +143,13 @@ pub struct VolumeServerState {
|
||||
}
|
||||
|
||||
impl VolumeServerState {
|
||||
/// The master lookups should go to: the leader the heartbeat confirmed,
|
||||
/// or empty while none is known, in its announced `host:port[.grpcPort]`
|
||||
/// form. Mirrors Go's `vs.getCurrentMaster()`.
|
||||
pub async fn current_master(&self) -> String {
|
||||
self.current_master_url.read().await.clone()
|
||||
}
|
||||
|
||||
/// Check if the server is in maintenance mode; return gRPC error if so.
|
||||
pub fn check_maintenance(&self) -> Result<(), tonic::Status> {
|
||||
if self.maintenance.load(Ordering::Relaxed) {
|
||||
@@ -209,7 +219,7 @@ impl VolumeServerState {
|
||||
return false;
|
||||
}
|
||||
let current = self.current_master_url.read().await.clone();
|
||||
if !current.is_empty() && current == key {
|
||||
if !current.is_empty() && to_http_address(¤t) == key {
|
||||
return true;
|
||||
}
|
||||
self.seed_master_set.contains(&key)
|
||||
|
||||
@@ -740,7 +740,11 @@ impl DiskLocation {
|
||||
if vol.is_read_only() {
|
||||
continue;
|
||||
}
|
||||
let dat_size = vol.dat_file_size().unwrap_or(0);
|
||||
// A preallocated volume already holds its space on disk.
|
||||
let dat_size = vol
|
||||
.dat_file_size()
|
||||
.unwrap_or(0)
|
||||
.max(vol.dat_allocated_size());
|
||||
let idx_size = vol.idx_file_size();
|
||||
let used = dat_size + idx_size;
|
||||
if volume_size_limit > used {
|
||||
|
||||
@@ -18,3 +18,4 @@ pub mod volume_idx_repair;
|
||||
pub mod volume_open;
|
||||
pub mod volume_report;
|
||||
pub mod volume_report_hash;
|
||||
pub mod volume_tail_repair;
|
||||
@@ -14,11 +14,11 @@ use crate::pb::master_pb;
|
||||
use crate::storage::disk_location::DiskLocation;
|
||||
use crate::storage::erasure_coding::ec_shard::{EcVolumeShard, MAX_SHARD_COUNT, ShardId};
|
||||
use crate::storage::erasure_coding::ec_volume::{
|
||||
ECJ_COMPACT_TMP_EXT, EcVolume, is_usable_ecx_file,
|
||||
ECJ_COMPACT_TMP_EXT, EcVolume, is_usable_ecx_file, read_ec_shard_config_across_dirs,
|
||||
};
|
||||
use crate::storage::needle::needle::{Needle, get_actual_size};
|
||||
use crate::storage::needle_map::NeedleMapKind;
|
||||
use crate::storage::super_block::{ReplicaPlacement, SUPER_BLOCK_SIZE};
|
||||
use crate::storage::super_block::{ReplicaPlacement, SUPER_BLOCK_SIZE, SuperBlock};
|
||||
use crate::storage::types::*;
|
||||
use crate::storage::volume::{CompactionJob, VifVolumeInfo, Volume, VolumeError, VolumeSpec};
|
||||
|
||||
@@ -416,6 +416,39 @@ impl Store {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Look up the per-volume EC data-shard count, preferring a mounted EC
|
||||
/// volume's own config and then the `.vif` on whichever disk holds it.
|
||||
/// Falls back to the default when neither exists yet (e.g. before the
|
||||
/// first shard streams in during ec.rebuild), so
|
||||
/// `find_ec_shard_target_location`'s free-slot maths scores custom-ratio
|
||||
/// volumes against their real width instead of the build default.
|
||||
pub fn lookup_ec_data_shards(&self, collection: &str, vid: VolumeId) -> u32 {
|
||||
use crate::storage::erasure_coding::ec_shard::DATA_SHARDS_COUNT;
|
||||
|
||||
for loc in &self.locations {
|
||||
if let Some(ec_vol) = loc.find_ec_volume(vid) {
|
||||
return ec_vol.data_shards;
|
||||
}
|
||||
}
|
||||
let Some(first) = self.locations.first() else {
|
||||
return DATA_SHARDS_COUNT as u32;
|
||||
};
|
||||
let other_dirs: Vec<String> = self.locations[1..]
|
||||
.iter()
|
||||
.flat_map(|loc| [loc.directory.clone(), loc.idx_directory.clone()])
|
||||
.filter(|d| !d.is_empty())
|
||||
.collect();
|
||||
read_ec_shard_config_across_dirs(
|
||||
&first.directory,
|
||||
&first.idx_directory,
|
||||
&other_dirs,
|
||||
collection,
|
||||
vid,
|
||||
)
|
||||
.map(|(data_shards, _, _)| data_shards)
|
||||
.unwrap_or(DATA_SHARDS_COUNT as u32)
|
||||
}
|
||||
|
||||
/// Create a new volume, placing it on the location with the most free space.
|
||||
pub fn add_volume(
|
||||
&mut self,
|
||||
@@ -729,8 +762,8 @@ impl Store {
|
||||
}
|
||||
|
||||
/// Configure a volume's replica placement on disk.
|
||||
/// The volume must already be unmounted. This opens the .dat file directly,
|
||||
/// modifies the replica_placement byte (offset 1), and writes it back.
|
||||
/// The volume must already be unmounted. Rewrites the .vif and the .dat
|
||||
/// superblock so both carry the new placement.
|
||||
pub fn configure_volume(&self, vid: VolumeId, rp: ReplicaPlacement) -> Result<(), VolumeError> {
|
||||
let (_, base_path, _) = self.find_volume_file_base(vid).ok_or_else(|| {
|
||||
VolumeError::Io(io::Error::new(
|
||||
@@ -742,7 +775,7 @@ impl Store {
|
||||
let mut vif = load_vif_volume_info(&vif_path)?;
|
||||
vif.replication = rp.to_string();
|
||||
save_vif_volume_info(&vif_path, &vif)?;
|
||||
Ok(())
|
||||
update_dat_replica_placement(&format!("{}.dat", base_path), vid, rp)
|
||||
}
|
||||
|
||||
// ---- Read / Write / Delete ----
|
||||
@@ -910,11 +943,7 @@ impl Store {
|
||||
let current = loc.max_volume_count.load(Ordering::Relaxed);
|
||||
let (_, free) = super::disk_location::get_disk_stats(&loc.directory);
|
||||
|
||||
let unused_space = if self.get_preallocate() {
|
||||
0
|
||||
} else {
|
||||
loc.unused_space(volume_size_limit)
|
||||
};
|
||||
let unused_space = loc.unused_space(volume_size_limit);
|
||||
let unclaimed = (free as i64) - (unused_space as i64);
|
||||
|
||||
let vol_count = loc.volumes_len() as i32;
|
||||
@@ -1711,6 +1740,61 @@ fn strip_volume_suffix(filename: &str) -> Option<&str> {
|
||||
.or_else(|| filename.strip_suffix(".idx"))
|
||||
}
|
||||
|
||||
/// Rewrites the replica placement in a .dat header for a volume that is not
|
||||
/// mounted; a missing .dat means a remote-only volume, where the .vif carries
|
||||
/// the placement.
|
||||
fn update_dat_replica_placement(
|
||||
dat_path: &str,
|
||||
vid: VolumeId,
|
||||
rp: ReplicaPlacement,
|
||||
) -> Result<(), VolumeError> {
|
||||
use std::io::{Read, Seek, SeekFrom, Write};
|
||||
|
||||
let mut file = match std::fs::OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open(dat_path)
|
||||
{
|
||||
Ok(f) => f,
|
||||
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(()),
|
||||
Err(e) => {
|
||||
return Err(VolumeError::Io(io::Error::new(
|
||||
e.kind(),
|
||||
format!("volume {} failed to open {}: {}", vid, dat_path, e),
|
||||
)))
|
||||
}
|
||||
};
|
||||
let mut header = [0u8; SUPER_BLOCK_SIZE];
|
||||
file.read_exact(&mut header).map_err(|e| {
|
||||
VolumeError::Io(io::Error::new(
|
||||
e.kind(),
|
||||
format!("volume {} failed to parse superblock: {}", vid, e),
|
||||
))
|
||||
})?;
|
||||
let extra_size = u16::from_be_bytes([header[6], header[7]]) as usize;
|
||||
let mut block = header.to_vec();
|
||||
block.resize(SUPER_BLOCK_SIZE + extra_size, 0);
|
||||
if extra_size > 0 {
|
||||
file.read_exact(&mut block[SUPER_BLOCK_SIZE..]).map_err(|e| {
|
||||
VolumeError::Io(io::Error::new(
|
||||
e.kind(),
|
||||
format!("volume {} failed to parse superblock: {}", vid, e),
|
||||
))
|
||||
})?;
|
||||
}
|
||||
let mut super_block = SuperBlock::from_bytes(&block).map_err(|e| {
|
||||
VolumeError::Io(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
format!("volume {} failed to parse superblock: {}", vid, e),
|
||||
))
|
||||
})?;
|
||||
super_block.replica_placement = rp;
|
||||
file.seek(SeekFrom::Start(0))?;
|
||||
file.write_all(&super_block.to_bytes())?;
|
||||
file.sync_all()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_vif_volume_info(path: &str) -> Result<VifVolumeInfo, VolumeError> {
|
||||
let content = match std::fs::read_to_string(path) {
|
||||
Ok(c) => c,
|
||||
@@ -1948,6 +2032,33 @@ mod tests {
|
||||
assert_eq!(store.total_volume_count(), 1);
|
||||
}
|
||||
|
||||
// Mirrors Go's TestConfigureVolumeUpdatesSuperblock: configure must leave
|
||||
// the .dat header and the .vif both on the new placement.
|
||||
#[test]
|
||||
fn test_configure_volume_updates_superblock() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let dir = tmp.path().to_str().unwrap();
|
||||
let mut store = make_test_store(&[dir]);
|
||||
|
||||
store
|
||||
.add_volume(VolumeId(1), DiskType::HardDrive, &VolumeSpec::default())
|
||||
.unwrap();
|
||||
assert!(store.unmount_volume(VolumeId(1)).unwrap());
|
||||
|
||||
let rp = ReplicaPlacement::from_string("010").unwrap();
|
||||
store.configure_volume(VolumeId(1), rp).unwrap();
|
||||
|
||||
let base = volume_file_name(dir, "", VolumeId(1));
|
||||
let vif = load_vif_volume_info(&format!("{}.vif", base)).unwrap();
|
||||
assert_eq!(vif.replication, "010");
|
||||
|
||||
let dat = std::fs::read(format!("{}.dat", base)).unwrap();
|
||||
let super_block = SuperBlock::from_bytes(&dat).unwrap();
|
||||
assert_eq!(super_block.replica_placement, rp);
|
||||
|
||||
assert!(store.configure_volume(VolumeId(2), rp).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mount_volume_by_id_hint_skips_lone_sidecar() {
|
||||
// A lone .vif/.idx sidecar (e.g. an EC sidecar whose .ecx is on a
|
||||
@@ -2446,7 +2557,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_maybe_adjust_volume_max_honors_preallocate_flag() {
|
||||
fn test_maybe_adjust_volume_max_reserves_plain_volumes_while_preallocating() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let dir = tmp.path().to_str().unwrap();
|
||||
|
||||
@@ -2461,13 +2572,6 @@ mod tests {
|
||||
Vec::new(),
|
||||
)
|
||||
.unwrap();
|
||||
// Use a large volume_size_limit so the unused-space difference between
|
||||
// preallocate=true (0) and preallocate=false (~2 × limit) is big enough
|
||||
// that integer-division rounding and disk-free fluctuations between the
|
||||
// two get_disk_stats calls cannot make the quotients equal.
|
||||
store
|
||||
.volume_size_limit
|
||||
.store(100 * 1024 * 1024, Ordering::Relaxed);
|
||||
store
|
||||
.add_volume(
|
||||
VolumeId(61),
|
||||
@@ -2478,39 +2582,67 @@ mod tests {
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Room for 2.5 volumes: the plain volume claims one, leaving one more slot.
|
||||
let (_, free) = crate::storage::disk_location::get_disk_stats(dir);
|
||||
let volume_size_limit = (free as f64 / 2.5) as u64;
|
||||
if volume_size_limit == 0 {
|
||||
return; // cannot determine free disk space
|
||||
}
|
||||
store
|
||||
.add_volume(
|
||||
VolumeId(62),
|
||||
.volume_size_limit
|
||||
.store(volume_size_limit, Ordering::Relaxed);
|
||||
store.locations[0].original_max_volume_count = 0;
|
||||
store.set_preallocate(true);
|
||||
|
||||
store.maybe_adjust_volume_max();
|
||||
assert_eq!(
|
||||
store.locations[0].max_volume_count.load(Ordering::Relaxed),
|
||||
2
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[test]
|
||||
fn test_unused_space_counts_preallocated_volumes_as_full() {
|
||||
const LIMIT: u64 = 1 << 20;
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let dir = tmp.path().to_str().unwrap();
|
||||
|
||||
let mut store = Store::new(NeedleMapKind::InMemory);
|
||||
store
|
||||
.add_location(
|
||||
dir,
|
||||
dir,
|
||||
2,
|
||||
DiskType::HardDrive,
|
||||
&VolumeSpec {
|
||||
collection: "preallocate_case",
|
||||
..Default::default()
|
||||
},
|
||||
MinFreeSpace::Percent(1.0),
|
||||
Vec::new(),
|
||||
)
|
||||
.unwrap();
|
||||
for vid in [VolumeId(61), VolumeId(62)] {
|
||||
let dat_path = store.find_volume(vid).unwrap().1.dat_path();
|
||||
std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.open(dat_path)
|
||||
.unwrap()
|
||||
.set_len((crate::storage::super_block::SUPER_BLOCK_SIZE + 1) as u64)
|
||||
for (vid, preallocate) in [(VolumeId(71), LIMIT), (VolumeId(72), 0)] {
|
||||
store
|
||||
.add_volume(
|
||||
vid,
|
||||
DiskType::HardDrive,
|
||||
&VolumeSpec {
|
||||
collection: "preallocate_case",
|
||||
preallocate,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
store.locations[0].original_max_volume_count = 0;
|
||||
store.locations[0]
|
||||
.max_volume_count
|
||||
.store(0, Ordering::Relaxed);
|
||||
let preallocated = store.find_volume(VolumeId(71)).unwrap().1;
|
||||
if preallocated.dat_allocated_size() < LIMIT {
|
||||
return; // the filesystem does not support fallocate
|
||||
}
|
||||
|
||||
store.set_preallocate(false);
|
||||
assert!(store.maybe_adjust_volume_max());
|
||||
let without_preallocate = store.locations[0].max_volume_count.load(Ordering::Relaxed);
|
||||
|
||||
store.set_preallocate(true);
|
||||
assert!(store.maybe_adjust_volume_max());
|
||||
let with_preallocate = store.locations[0].max_volume_count.load(Ordering::Relaxed);
|
||||
|
||||
assert!(with_preallocate > without_preallocate);
|
||||
let plain = store.find_volume(VolumeId(72)).unwrap().1;
|
||||
let plain_dat = plain.dat_file_size().unwrap();
|
||||
// The filesystem may round a small file up to a larger allocation unit.
|
||||
let plain_used = plain_dat.max(plain.dat_allocated_size()) + plain.idx_file_size();
|
||||
assert_eq!(store.locations[0].unused_space(LIMIT), LIMIT - plain_used);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -2980,6 +3112,35 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
/// A volume whose `.vif` declares a custom EC ratio must feed its own
|
||||
/// data-shard count into the free-slot maths — not the build default —
|
||||
/// wherever the `.vif` happens to sit. With no `.vif` anywhere (a
|
||||
/// brand-new volume mid-ec.rebuild) the answer is the default.
|
||||
#[test]
|
||||
fn test_lookup_ec_data_shards_reads_vif_on_any_disk() {
|
||||
use crate::storage::erasure_coding::ec_shard::DATA_SHARDS_COUNT;
|
||||
|
||||
let (store, _tmp) = make_ec_target_test_store(3);
|
||||
let collection = "custom-ratio";
|
||||
let vid = VolumeId(4242);
|
||||
|
||||
assert_eq!(
|
||||
store.lookup_ec_data_shards(collection, vid),
|
||||
DATA_SHARDS_COUNT as u32,
|
||||
"no .vif anywhere must answer the default"
|
||||
);
|
||||
|
||||
// A 6+2 .vif lands on disk 2 only — the rebuild staging disk.
|
||||
let base = volume_file_name(&store.locations[2].directory, collection, vid);
|
||||
std::fs::write(
|
||||
format!("{}.vif", base),
|
||||
br#"{"ecShardConfig":{"dataShards":6,"parityShards":2}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(store.lookup_ec_data_shards(collection, vid), 6);
|
||||
}
|
||||
|
||||
/// Deleting a disk's last shard removes that disk's now-orphaned `.ecsum`;
|
||||
/// a disk this delete never changed keeps its sidecar. Disk 0 models
|
||||
/// ec.rebuild's index-prep state (`.ecsum` landed, no shards yet) — an
|
||||
|
||||
@@ -29,6 +29,7 @@ use crate::storage::needle_map::{CompactNeedleMap, NeedleMap, NeedleMapKind, Red
|
||||
use crate::storage::super_block::{ReplicaPlacement, SUPER_BLOCK_SIZE, SuperBlock};
|
||||
use crate::storage::types::*;
|
||||
use crate::storage::volume_open::open_volume_file;
|
||||
use crate::storage::volume_tail_repair::TailRepairError;
|
||||
|
||||
// ============================================================================
|
||||
// Errors
|
||||
@@ -1247,6 +1248,12 @@ pub struct Volume {
|
||||
idx_syncs_for_test: std::sync::atomic::AtomicUsize,
|
||||
needle_map_kind: NeedleMapKind,
|
||||
data_file_access_control: Arc<DataFileAccessControl>,
|
||||
/// Serializes a data-file mutation with the sync that makes it durable:
|
||||
/// write, delete and lifecycle paths hold it for their whole critical
|
||||
/// section, while the fsync itself runs with only the `io` read guard so
|
||||
/// readers of this volume do not wait on it. Lock order is
|
||||
/// `data_file_sync_lock` -> `data_file_access_control` -> `io`.
|
||||
data_file_sync_lock: std::sync::Mutex<()>,
|
||||
|
||||
/// Go's superBlockAccessLock.
|
||||
super_block: RwLock<SuperBlock>,
|
||||
@@ -1370,6 +1377,7 @@ impl Volume {
|
||||
idx_syncs_for_test: Default::default(),
|
||||
needle_map_kind,
|
||||
data_file_access_control: Arc::new(DataFileAccessControl::default()),
|
||||
data_file_sync_lock: std::sync::Mutex::new(()),
|
||||
super_block: RwLock::new(SuperBlock {
|
||||
replica_placement: replica_placement.unwrap_or_default(),
|
||||
ttl: ttl.unwrap_or(crate::storage::needle::ttl::TTL::EMPTY),
|
||||
@@ -1426,6 +1434,7 @@ impl Volume {
|
||||
idx_syncs_for_test: Default::default(),
|
||||
needle_map_kind: NeedleMapKind::InMemory,
|
||||
data_file_access_control: Arc::new(DataFileAccessControl::default()),
|
||||
data_file_sync_lock: std::sync::Mutex::new(()),
|
||||
super_block: RwLock::new(SuperBlock::default()),
|
||||
no_write: AtomicU8::new(0),
|
||||
io_unavailable: Arc::default(),
|
||||
@@ -1446,6 +1455,13 @@ impl Volume {
|
||||
self.data_file_access_control.clone()
|
||||
}
|
||||
|
||||
/// The mutation/sync serializer, for callers that manage the access
|
||||
/// lease themselves (the `_held` lifecycle paths). Same lock order as
|
||||
/// everywhere else: sync lock first, then the write lease, then `io`.
|
||||
pub(crate) fn data_file_sync_lock(&self) -> &std::sync::Mutex<()> {
|
||||
&self.data_file_sync_lock
|
||||
}
|
||||
|
||||
pub(crate) fn is_instance(&self, instance: &Arc<DataFileAccessControl>) -> bool {
|
||||
Arc::ptr_eq(instance, &self.data_file_access_control)
|
||||
}
|
||||
@@ -1709,6 +1725,48 @@ impl Volume {
|
||||
),
|
||||
}
|
||||
|
||||
// A crash between the .dat append and the .idx append leaves a
|
||||
// .dat tail no index entry covers. Re-index complete needles and
|
||||
// cut an incomplete remainder before the index loads, so the
|
||||
// recovered rows land in the needle map and inside its append
|
||||
// offset. Mirrors Go running repairUnindexedDatTail inside
|
||||
// CheckVolumeDataIntegrity ahead of the needle map load. Only a
|
||||
// volume about to load writable is repaired.
|
||||
// A no_write_can_delete volume still appends tombstone records on
|
||||
// delete, so it tears tails the same way and gets the same repair.
|
||||
if !self.has_remote_file() && !self.no_write_flags().0 {
|
||||
// An empty .idx gets the same repair: every check below
|
||||
// skips an empty index on its own, so a tail the repair
|
||||
// cannot explain there fails the load like in Go rather
|
||||
// than warn and load writable.
|
||||
let idx_empty = fs::metadata(self.file_name(".idx"))
|
||||
.map(|m| m.len() == 0)
|
||||
.unwrap_or(false);
|
||||
match self.repair_unindexed_dat_tail(io) {
|
||||
Ok(()) => {}
|
||||
Err(e) if e.is_not_torn() && !idx_empty => warn!(
|
||||
volume_id = self.id.0,
|
||||
error = %e,
|
||||
"unindexed .dat tail not repaired"
|
||||
),
|
||||
// The index refusing a read-write open is the same failure
|
||||
// Go reports opening it ahead of the check: it fails the
|
||||
// load rather than only the repair.
|
||||
Err(TailRepairError::OpenIndex(e)) => return Err(e),
|
||||
Err(e) => {
|
||||
// A repair that could not complete must not load the
|
||||
// volume writable on files it half-fixed: the check
|
||||
// fails as in Go, so the volume loads read-only.
|
||||
self.set_no_write_flag(NO_WRITE_OR_DELETE, true);
|
||||
warn!(
|
||||
volume_id = self.id.0,
|
||||
error = %e,
|
||||
"repair unindexed .dat tail"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.load_index(io)?;
|
||||
|
||||
// Match Go: CheckVolumeDataIntegrity after loading index (volume_loading.go L154-159)
|
||||
@@ -1980,6 +2038,7 @@ impl Volume {
|
||||
}
|
||||
|
||||
pub(crate) fn load_remote_dat_file(&self) -> Result<(), VolumeError> {
|
||||
let _sync_guard = self.data_file_sync_lock.lock().unwrap();
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
let mut io = self.io.write().unwrap();
|
||||
self.load_remote_dat_file_io(&mut io)
|
||||
@@ -2066,7 +2125,7 @@ impl Volume {
|
||||
self.current_dat_file_size_io(&io)
|
||||
}
|
||||
|
||||
fn current_dat_file_size_io(&self, io: &VolumeIo) -> io::Result<u64> {
|
||||
pub(crate) fn current_dat_file_size_io(&self, io: &VolumeIo) -> io::Result<u64> {
|
||||
if let Some(ref f) = io.dat_file {
|
||||
Ok(f.metadata()?.len())
|
||||
} else if let Some(ref remote_dat_file) = io.remote_dat_file {
|
||||
@@ -2290,7 +2349,7 @@ impl Volume {
|
||||
self.read_needle_blob_unlocked(&io, offset, size)
|
||||
}
|
||||
|
||||
fn read_needle_blob_unlocked(
|
||||
pub(crate) fn read_needle_blob_unlocked(
|
||||
&self,
|
||||
io: &VolumeIo,
|
||||
offset: i64,
|
||||
@@ -2509,10 +2568,14 @@ impl Volume {
|
||||
check_cookie: bool,
|
||||
fsync: bool,
|
||||
) -> Result<(u64, Size, bool), VolumeError> {
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
let mut io = self.io.write().unwrap();
|
||||
self.check_writable()?;
|
||||
self.do_write_request(&mut io, n, check_cookie, fsync)
|
||||
// A single write is a one-entry run of the grouped path: identical
|
||||
// outcome, and the fsync handling lives in one place.
|
||||
debug_assert!(check_cookie, "every write_needle caller checks the cookie");
|
||||
let _ = check_cookie;
|
||||
let mut writes = [(std::mem::take(n), fsync)];
|
||||
let mut results = self.write_needles_grouped(&mut writes);
|
||||
*n = std::mem::replace(&mut writes[0].0, Needle::default());
|
||||
results.pop().unwrap_or(Err(VolumeError::NotFound))
|
||||
}
|
||||
|
||||
/// Write a batch of needles the way Go's processBatch does: one .dat
|
||||
@@ -2527,8 +2590,13 @@ impl Volume {
|
||||
&self,
|
||||
writes: &mut [(Needle, bool)],
|
||||
) -> Vec<Result<(u64, Size, bool), VolumeError>> {
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
let mut io = self.io.write().unwrap();
|
||||
// The sync lock serializes a .dat mutation with the flush that makes
|
||||
// it durable: a failed sync's rollback can never clobber an append
|
||||
// that slipped in behind it, and a compaction swap or unlink cannot
|
||||
// land between the append and its sync. Readers never take it, and
|
||||
// the write-side guards are dropped for the sync itself, so reads of
|
||||
// this volume do not wait on fsync.
|
||||
let _sync_guard = self.data_file_sync_lock.lock().unwrap();
|
||||
let mut results = Vec::with_capacity(writes.len());
|
||||
let mut rest = writes;
|
||||
while !rest.is_empty() {
|
||||
@@ -2536,8 +2604,10 @@ impl Volume {
|
||||
let len = rest.iter().take_while(|(n, _)| ids.insert(n.id)).count();
|
||||
let (run, tail) = std::mem::take(&mut rest).split_at_mut(len);
|
||||
if run.iter().any(|(_, fsync)| *fsync) {
|
||||
results.extend(self.write_synced_run(&mut io, run));
|
||||
results.extend(self.write_synced_run(run));
|
||||
} else {
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
let mut io = self.io.write().unwrap();
|
||||
for (n, _) in run.iter_mut() {
|
||||
results.push(
|
||||
self.check_writable()
|
||||
@@ -2552,7 +2622,6 @@ impl Volume {
|
||||
|
||||
fn write_synced_run(
|
||||
&self,
|
||||
io: &mut VolumeIo,
|
||||
run: &mut [(Needle, bool)],
|
||||
) -> Vec<Result<(u64, Size, bool), VolumeError>> {
|
||||
// Per entry: Some(offset) once appended, None when it dedups.
|
||||
@@ -2560,63 +2629,82 @@ impl Volume {
|
||||
// Per entry: the I/O error streak once it is staged, where a write
|
||||
// sent on its own would have recorded its success.
|
||||
let mut marks = Vec::with_capacity(run.len());
|
||||
let mut last_append_at_ns = io.last_append_at_ns;
|
||||
let mut last_append_at_ns;
|
||||
let mut run_start = None;
|
||||
let mut sync = false;
|
||||
for (n, fsync) in run.iter_mut() {
|
||||
let r = self.append_unpublished(io, n, &mut last_append_at_ns);
|
||||
marks.push(self.io_errors.mark());
|
||||
if let Ok(Some(offset)) = r {
|
||||
run_start.get_or_insert(offset);
|
||||
}
|
||||
// Only a durable entry that got this far needs the sync.
|
||||
sync |= *fsync && r.is_ok();
|
||||
staged.push(r);
|
||||
}
|
||||
|
||||
if sync && let Err(e) = self.flush_dat(io) {
|
||||
self.check_read_write_error(Some(&e));
|
||||
if let Some(start) = run_start {
|
||||
self.undo_unsynced_append(io, start);
|
||||
}
|
||||
let e = VolumeError::Io(e);
|
||||
return staged
|
||||
.into_iter()
|
||||
.map(|r| r.and_then(|_| Err(run_error(&e))))
|
||||
.collect();
|
||||
}
|
||||
io.last_append_at_ns = last_append_at_ns;
|
||||
for ((n, _), r) in run.iter().zip(&staged) {
|
||||
if matches!(r, Ok(Some(_))) && n.append_at_ns > io.last_write_append_at_ns {
|
||||
io.last_write_append_at_ns = n.append_at_ns;
|
||||
io.last_write_needle_key = n.id;
|
||||
io.last_write_deleted = false;
|
||||
{
|
||||
// Append under the write guards, then drop them for the flush:
|
||||
// readers need only `io`'s read side, while mutators and a
|
||||
// compaction swap stay excluded by the caller's sync lock.
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
let mut io = self.io.write().unwrap();
|
||||
last_append_at_ns = io.last_append_at_ns;
|
||||
for (n, fsync) in run.iter_mut() {
|
||||
let r = self.append_unpublished(&mut io, n, &mut last_append_at_ns);
|
||||
marks.push(self.io_errors.mark());
|
||||
if let Ok(Some(offset)) = r {
|
||||
run_start.get_or_insert(offset);
|
||||
}
|
||||
// Only a durable entry that got this far needs the sync.
|
||||
sync |= *fsync && r.is_ok();
|
||||
staged.push(r);
|
||||
}
|
||||
}
|
||||
|
||||
// A durable entry that fails to publish stops the volume taking
|
||||
// writes, so the entries after it are refused the way a lone
|
||||
// write would be.
|
||||
let mut refused = false;
|
||||
for ((n, fsync), r) in run.iter().zip(staged.iter_mut()) {
|
||||
if refused {
|
||||
*r = Err(self
|
||||
.check_writable()
|
||||
.err()
|
||||
.unwrap_or(VolumeError::ReadOnly(self.id)));
|
||||
} else if let Ok(Some(offset)) = *r
|
||||
&& let Err(e) = self.publish_write(io, n, offset, *fsync)
|
||||
{
|
||||
*r = Err(e);
|
||||
refused = *fsync;
|
||||
if sync {
|
||||
let io = self.io.read().unwrap();
|
||||
if let Err(e) = self.flush_dat(&io) {
|
||||
self.check_read_write_error(Some(&e));
|
||||
if let Some(start) = run_start {
|
||||
self.undo_unsynced_append(&io, start);
|
||||
}
|
||||
let e = VolumeError::Io(e);
|
||||
return staged
|
||||
.into_iter()
|
||||
.map(|r| r.and_then(|_| Err(run_error(&e))))
|
||||
.collect();
|
||||
}
|
||||
}
|
||||
|
||||
if sync && let Err(e) = self.flush_idx(io) {
|
||||
return staged
|
||||
.into_iter()
|
||||
.map(|r| r.and_then(|_| Err(run_error(&e))))
|
||||
.collect();
|
||||
{
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
let mut io = self.io.write().unwrap();
|
||||
io.last_append_at_ns = last_append_at_ns;
|
||||
for ((n, _), r) in run.iter().zip(&staged) {
|
||||
if matches!(r, Ok(Some(_))) && n.append_at_ns > io.last_write_append_at_ns {
|
||||
io.last_write_append_at_ns = n.append_at_ns;
|
||||
io.last_write_needle_key = n.id;
|
||||
io.last_write_deleted = false;
|
||||
}
|
||||
}
|
||||
|
||||
// A durable entry that fails to publish stops the volume taking
|
||||
// writes, so the entries after it are refused the way a lone
|
||||
// write would be.
|
||||
let mut refused = false;
|
||||
for ((n, fsync), r) in run.iter().zip(staged.iter_mut()) {
|
||||
if refused {
|
||||
*r = Err(self
|
||||
.check_writable()
|
||||
.err()
|
||||
.unwrap_or(VolumeError::ReadOnly(self.id)));
|
||||
} else if let Ok(Some(offset)) = *r
|
||||
&& let Err(e) = self.publish_write(&mut io, n, offset, *fsync)
|
||||
{
|
||||
*r = Err(e);
|
||||
refused = *fsync;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if sync {
|
||||
let io = self.io.read().unwrap();
|
||||
if let Err(e) = self.flush_idx(&io) {
|
||||
return staged
|
||||
.into_iter()
|
||||
.map(|r| r.and_then(|_| Err(run_error(&e))))
|
||||
.collect();
|
||||
}
|
||||
}
|
||||
|
||||
let written = run
|
||||
@@ -2630,7 +2718,9 @@ impl Volume {
|
||||
// Sent one at a time, the last write to land would have cleared
|
||||
// the streak of the appends before it, and the ones after it
|
||||
// would have failed on top of that.
|
||||
self.finish_write(io, last_modified, sync, marks[last]);
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
let mut io = self.io.write().unwrap();
|
||||
self.finish_write(&mut io, last_modified, sync, marks[last]);
|
||||
}
|
||||
|
||||
run.iter()
|
||||
@@ -2679,7 +2769,7 @@ impl Volume {
|
||||
/// Flush the .dat, the first half of a durable write. The .idx is flushed
|
||||
/// separately by flush_idx once the row is published; the two are split so
|
||||
/// nothing is indexed before the bytes it points at are down.
|
||||
fn flush_dat(&self, io: &VolumeIo) -> io::Result<()> {
|
||||
pub(crate) fn flush_dat(&self, io: &VolumeIo) -> io::Result<()> {
|
||||
#[cfg(test)]
|
||||
self.dat_syncs_for_test.fetch_add(1, Ordering::Relaxed);
|
||||
#[cfg(test)]
|
||||
@@ -2731,7 +2821,7 @@ impl Volume {
|
||||
}
|
||||
|
||||
/// Take an unflushed append back off the .dat after its sync failed.
|
||||
fn truncate_dat(&self, io: &VolumeIo, len: u64) -> io::Result<()> {
|
||||
pub(crate) fn truncate_dat(&self, io: &VolumeIo, len: u64) -> io::Result<()> {
|
||||
#[cfg(test)]
|
||||
if io.fail_truncate_for_test {
|
||||
return Err(io::Error::other("injected truncate failure"));
|
||||
@@ -3082,6 +3172,7 @@ impl Volume {
|
||||
|
||||
/// Delete a needle from the volume.
|
||||
pub fn delete_needle(&self, n: &mut Needle) -> Result<Size, VolumeError> {
|
||||
let _sync_guard = self.data_file_sync_lock.lock().unwrap();
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
let mut io = self.io.write().unwrap();
|
||||
if self.is_closed() {
|
||||
@@ -3464,7 +3555,7 @@ impl Volume {
|
||||
/// key-sorted .idx (e.g. legacy weed fix output) falls back to a single
|
||||
/// linear scan for the maximum offset. Returns -1 when no entry has a
|
||||
/// non-zero offset (a pre-allocated / all-zero index).
|
||||
fn find_dat_tail_entry_offset(
|
||||
pub(crate) fn find_dat_tail_entry_offset(
|
||||
&self,
|
||||
io: &VolumeIo,
|
||||
idx_file: &mut File,
|
||||
@@ -3981,6 +4072,7 @@ impl Volume {
|
||||
offset: Offset,
|
||||
size: Size,
|
||||
) -> Result<(), VolumeError> {
|
||||
let _sync_guard = self.data_file_sync_lock.lock().unwrap();
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
let mut io = self.io.write().unwrap();
|
||||
if let Some(ref mut nm) = io.nm {
|
||||
@@ -4005,6 +4097,7 @@ impl Volume {
|
||||
// The flag flip and the .vif save are one critical section with the idx
|
||||
// attach: Go's markVolumeReadonly runs under dataFileAccessLock, so a
|
||||
// racing mark-writable cannot interleave a save of half-applied state.
|
||||
let _sync_guard = self.data_file_sync_lock.lock().unwrap();
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
self.set_read_only_persist_held(can_delete, persist)
|
||||
}
|
||||
@@ -4089,10 +4182,13 @@ impl Volume {
|
||||
if let Some(e) = self.unavailable_error() {
|
||||
return Err(e);
|
||||
}
|
||||
let _sync_guard = self.data_file_sync_lock.lock().unwrap();
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
let (was_no_write_or_delete, was_no_write_can_delete) = self.no_write_flags();
|
||||
// Remote-tiered volumes must stay no_write_can_delete regardless of marks.
|
||||
let no_write_can_delete = was_no_write_can_delete && self.has_remote_file();
|
||||
// A remote-tiered volume keeps the delete-only mode a load derives: its
|
||||
// .dat cannot take appends, so "writable" resolves to can-delete rather
|
||||
// than advertising writable with no local data file.
|
||||
let no_write_can_delete = self.has_remote_file();
|
||||
self.no_write.store(
|
||||
if no_write_can_delete {
|
||||
NO_WRITE_CAN_DELETE
|
||||
@@ -4187,6 +4283,7 @@ impl Volume {
|
||||
&self,
|
||||
f: impl FnOnce(&mut Vec<PbRemoteFile>),
|
||||
) -> Result<(), VolumeError> {
|
||||
let _sync_guard = self.data_file_sync_lock.lock().unwrap();
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
self.update_remote_files_held(f)
|
||||
}
|
||||
@@ -4205,21 +4302,70 @@ impl Volume {
|
||||
}
|
||||
f(&mut self.volume_info.write().unwrap().files);
|
||||
let mut io = self.io.write().unwrap();
|
||||
self.refresh_remote_write_mode(&mut io)
|
||||
self.refresh_remote_write_mode(&mut io, true)
|
||||
}
|
||||
|
||||
/// Go's MarkTieredDeletable: a live tier-up lifts the upload-time
|
||||
/// read-only mark, except on a replicated volume — the shell copies the
|
||||
/// uploaded replica's .idx and .vif to the other replicas after the
|
||||
/// upload, and a tombstone written before that copy would reach one
|
||||
/// replica only — or under an I/O quarantine, which is not the tier
|
||||
/// upload's to lift. A refused volume keeps its flags until the next
|
||||
/// load, as the Go volume does.
|
||||
fn mark_tiered_deletable(&self) -> bool {
|
||||
if self
|
||||
.super_block
|
||||
.read()
|
||||
.unwrap()
|
||||
.replica_placement
|
||||
.has_replication()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if !self.has_remote_file() {
|
||||
return false;
|
||||
}
|
||||
// Go takes noWriteLock around the quarantine check and the flag
|
||||
// change; here a compare-exchange gives the same ordering — a
|
||||
// heartbeat refusal landing between the check and the swap fails the
|
||||
// swap, and the retry sees the quarantine mark the heartbeat set
|
||||
// first.
|
||||
loop {
|
||||
if self.should_quarantine() {
|
||||
return false;
|
||||
}
|
||||
let flags = self.no_write.load(Ordering::Acquire);
|
||||
let promoted = (flags & !NO_WRITE_OR_DELETE) | NO_WRITE_CAN_DELETE;
|
||||
if self
|
||||
.no_write
|
||||
.compare_exchange(flags, promoted, Ordering::AcqRel, Ordering::Acquire)
|
||||
.is_ok()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Recompute the Go-style write/delete mode from the current remote tier
|
||||
/// state, and bring the needle map in line with it — a volume that stops
|
||||
/// being remote also stops using the read-only sorted index.
|
||||
///
|
||||
/// `live` is a tier transition on a running volume: the promotion then
|
||||
/// goes through mark_tiered_deletable, Go's MarkTieredDeletable, while a
|
||||
/// load promotes every tiered volume unconditionally as Go's load() does.
|
||||
///
|
||||
/// If the map cannot be rebuilt the volume is pinned read-only rather than
|
||||
/// published as writable with an index that rejects every put; a restart
|
||||
/// recovers it.
|
||||
fn refresh_remote_write_mode(&self, io: &mut VolumeIo) -> Result<(), VolumeError> {
|
||||
fn refresh_remote_write_mode(&self, io: &mut VolumeIo, live: bool) -> Result<(), VolumeError> {
|
||||
let has_remote = self.has_remote_file();
|
||||
if has_remote {
|
||||
// remote-tiered: no writes, deletes allowed
|
||||
self.no_write.store(NO_WRITE_CAN_DELETE, Ordering::Release);
|
||||
if !live {
|
||||
self.no_write.store(NO_WRITE_CAN_DELETE, Ordering::Release);
|
||||
} else {
|
||||
self.mark_tiered_deletable();
|
||||
}
|
||||
} else if !self.volume_info.read().unwrap().read_only_can_delete {
|
||||
// only clear the remoteness-derived flag, not an operator mark
|
||||
self.set_no_write_flag(NO_WRITE_CAN_DELETE, false);
|
||||
@@ -4268,7 +4414,7 @@ impl Volume {
|
||||
}
|
||||
}
|
||||
*self.volume_info.write().unwrap() = pb_info;
|
||||
self.refresh_remote_write_mode(io)?;
|
||||
self.refresh_remote_write_mode(io, false)?;
|
||||
{
|
||||
let mut vi = self.volume_info.write().unwrap();
|
||||
if vi.version == 0 {
|
||||
@@ -4299,7 +4445,7 @@ impl Volume {
|
||||
self.set_no_write_flag(NO_WRITE_OR_DELETE, true);
|
||||
}
|
||||
*self.volume_info.write().unwrap() = pb_info;
|
||||
self.refresh_remote_write_mode(io)?;
|
||||
self.refresh_remote_write_mode(io, false)?;
|
||||
{
|
||||
let mut vi = self.volume_info.write().unwrap();
|
||||
if vi.version == 0 {
|
||||
@@ -4451,6 +4597,7 @@ impl Volume {
|
||||
|
||||
/// Change the replication placement and rewrite the super block.
|
||||
pub fn set_replica_placement(&self, rp: ReplicaPlacement) -> Result<(), VolumeError> {
|
||||
let _sync_guard = self.data_file_sync_lock.lock().unwrap();
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
let bytes = {
|
||||
let mut sb = self.super_block.write().unwrap();
|
||||
@@ -4621,6 +4768,7 @@ impl Volume {
|
||||
|
||||
/// Write a raw needle blob at a specific offset in the .dat file.
|
||||
pub fn write_needle_blob(&self, offset: i64, needle_blob: &[u8]) -> Result<(), VolumeError> {
|
||||
let _sync_guard = self.data_file_sync_lock.lock().unwrap();
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
if self.is_read_only() {
|
||||
return Err(VolumeError::ReadOnly(self.id));
|
||||
@@ -4652,6 +4800,7 @@ impl Volume {
|
||||
needle_blob: &[u8],
|
||||
size: Size,
|
||||
) -> Result<(), VolumeError> {
|
||||
let _sync_guard = self.data_file_sync_lock.lock().unwrap();
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
// nm.put on a read-only volume fails only after the blob is appended to .dat.
|
||||
if self.is_read_only() {
|
||||
@@ -4836,6 +4985,25 @@ impl Volume {
|
||||
self.current_dat_file_size()
|
||||
}
|
||||
|
||||
/// Disk space held by the local .dat file, or 0 when it is unknown. It
|
||||
/// exceeds the file size when the file was preallocated with
|
||||
/// `FALLOC_FL_KEEP_SIZE`.
|
||||
pub fn dat_allocated_size(&self) -> u64 {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
let io = self.io.read().unwrap();
|
||||
io.dat_file
|
||||
.as_ref()
|
||||
.and_then(|f| f.metadata().ok())
|
||||
.map_or(0, |m| m.blocks() * 512)
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the modification time of the .dat file as Unix seconds.
|
||||
pub fn dat_file_mod_time(&self) -> u64 {
|
||||
let io = self.io.read().unwrap();
|
||||
@@ -4956,6 +5124,16 @@ impl Volume {
|
||||
// would stall every volume behind one slow client. The vacuum-commit
|
||||
// RPC uses the lease-holding path instead — it waits with no store
|
||||
// guard held, matching Go's blocking commit.
|
||||
// The sync lock must come before the lease and be non-blocking like
|
||||
// it: a durable write holds it across its fsync, and the commit must
|
||||
// not swap files over an unpublished write — nor stall the store
|
||||
// behind one.
|
||||
let Ok(_sync_guard) = self.data_file_sync_lock.try_lock() else {
|
||||
return Err(VolumeError::Io(io::Error::new(
|
||||
io::ErrorKind::WouldBlock,
|
||||
COMMIT_WAITS_FOR_READS,
|
||||
)));
|
||||
};
|
||||
let Some(_lease) = self.data_file_access_control.try_write_lock() else {
|
||||
return Err(VolumeError::Io(io::Error::new(
|
||||
io::ErrorKind::WouldBlock,
|
||||
@@ -4965,7 +5143,9 @@ impl Volume {
|
||||
self.commit_compact_held()
|
||||
}
|
||||
|
||||
/// `commit_compact` for callers already holding the write lease.
|
||||
/// `commit_compact` for callers already holding `data_file_sync_lock`
|
||||
/// and the write lease — the swap must not land between a durable
|
||||
/// write's append and its publish.
|
||||
pub(crate) fn commit_compact_held(&self) -> Result<(), VolumeError> {
|
||||
// A handle detached between lookup and lease must not swap compacted
|
||||
// files over the ones its remounted replacement now owns.
|
||||
@@ -5356,6 +5536,7 @@ impl Volume {
|
||||
/// Go's `Volume.Close`: the write lease excludes in-flight I/O — with a
|
||||
/// shared handle, `&mut self` no longer provides that exclusion.
|
||||
pub fn close(&self) {
|
||||
let _sync_guard = self.data_file_sync_lock.lock().unwrap();
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
self.closed.store(true, Ordering::Release);
|
||||
self.close_locked();
|
||||
@@ -5401,6 +5582,7 @@ impl Volume {
|
||||
if self.is_compacting() {
|
||||
return Err(self.compacting_error());
|
||||
}
|
||||
let _sync_guard = self.data_file_sync_lock.lock().unwrap();
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
|
||||
if self.dir_idx() == new_idx_dir {
|
||||
@@ -5463,6 +5645,7 @@ impl Volume {
|
||||
) -> Result<(), VolumeError> {
|
||||
// Go's Destroy holds dataFileAccessLock: file unlinking must exclude
|
||||
// in-flight I/O from a shared handle, not just borrow exclusivity.
|
||||
let _sync_guard = self.data_file_sync_lock.lock().unwrap();
|
||||
let _guard = self.data_file_access_control.write_lock();
|
||||
// Either enabled check may pass: a volume with no live data qualifies
|
||||
// whether it reads empty or as all garbage. Byte counters, not counts:
|
||||
@@ -9588,6 +9771,72 @@ mod tests {
|
||||
assert!(!v.is_no_write_or_delete());
|
||||
}
|
||||
|
||||
/// Go's MarkTieredDeletable leaves a replicated volume's upload-time
|
||||
/// read-only mark alone: the shell still has to copy the uploaded
|
||||
/// replica's .idx to the others, and a tombstone written before that copy
|
||||
/// would reach one replica only.
|
||||
#[test]
|
||||
fn test_update_remote_files_keeps_a_replicated_volume_refusing_deletes() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let dir = tmp.path().to_str().unwrap();
|
||||
let v = make_test_volume(dir);
|
||||
v.super_block.write().unwrap().replica_placement =
|
||||
crate::storage::super_block::ReplicaPlacement::from_string("001").unwrap();
|
||||
v.set_no_write_or_delete(true); // the shell's pre-upload mark
|
||||
|
||||
v.update_remote_files(|files| files.push(Default::default()))
|
||||
.unwrap();
|
||||
|
||||
assert!(v.has_remote_file());
|
||||
assert!(
|
||||
v.is_no_write_or_delete(),
|
||||
"a replicated volume keeps refusing deletes until its next load"
|
||||
);
|
||||
assert!(!v.is_no_write_can_delete());
|
||||
}
|
||||
|
||||
/// An I/O quarantine is not the tier upload's to lift: a live tier-up
|
||||
/// leaves a quarantined volume's read-only mark alone, as Go's
|
||||
/// MarkTieredDeletable does.
|
||||
#[test]
|
||||
fn test_update_remote_files_keeps_an_io_quarantine() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let dir = tmp.path().to_str().unwrap();
|
||||
let v = make_test_volume(dir);
|
||||
v.set_no_write_or_delete(true);
|
||||
v.mark_io_quarantined();
|
||||
|
||||
v.update_remote_files(|files| files.push(Default::default()))
|
||||
.unwrap();
|
||||
|
||||
assert!(v.has_remote_file());
|
||||
assert!(v.is_no_write_or_delete());
|
||||
assert!(!v.is_no_write_can_delete());
|
||||
}
|
||||
|
||||
/// A refused tiered volume keeps OR_DELETE without CAN_DELETE; marking it
|
||||
/// writable must resolve to the delete-only mode a load derives — the
|
||||
/// remote .dat cannot take appends — not drop every protection.
|
||||
#[test]
|
||||
fn test_set_writable_on_a_refused_tiered_volume_stays_delete_only() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let dir = tmp.path().to_str().unwrap();
|
||||
let v = make_test_volume(dir);
|
||||
v.super_block.write().unwrap().replica_placement =
|
||||
crate::storage::super_block::ReplicaPlacement::from_string("001").unwrap();
|
||||
v.set_no_write_or_delete(true);
|
||||
|
||||
v.update_remote_files(|files| files.push(Default::default()))
|
||||
.unwrap();
|
||||
assert!(v.is_no_write_or_delete());
|
||||
|
||||
v.set_writable().unwrap();
|
||||
|
||||
assert!(v.is_read_only());
|
||||
assert!(!v.no_write_flags().0);
|
||||
assert!(v.no_write_flags().1);
|
||||
}
|
||||
|
||||
// Tier-down clears the remote mode and publishes the volume as writable. The
|
||||
// map it booted with is the read-only sorted one, whose put always fails, so
|
||||
// without a rebuild the first write appends to .dat and then cannot be
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -110,6 +110,21 @@ func startFailoverCluster(t testing.TB, numVolumes, numMounts int) *failoverClus
|
||||
"mount %d not ready\n%s", i, c.tailLog(fmt.Sprintf("mount%d", i)))
|
||||
}
|
||||
require.NoError(t, c.WaitForVolumeServers(60*time.Second))
|
||||
|
||||
// A mounted filesystem and registered volume servers do not yet guarantee
|
||||
// that the lock ring has settled or the first replicated volume is ready.
|
||||
// Exercise the write/read path during setup so the append latency checks
|
||||
// measure failover on a running cluster rather than its cold start.
|
||||
probeName := ".failover-ready"
|
||||
probeData := []byte("ready\n")
|
||||
probePath := filepath.Join(c.MountDir(0), probeName)
|
||||
require.NoError(t, os.WriteFile(probePath, probeData, 0644),
|
||||
"readiness write failed\n%s", c.tailLog("mount0"))
|
||||
for i := 0; i < numMounts; i++ {
|
||||
_, ready := waitForContent(filepath.Join(c.MountDir(i), probeName), probeData, convergeTimeout)
|
||||
require.True(t, ready, "mount %d did not observe readiness write\n%s", i, c.tailLog(fmt.Sprintf("mount%d", i)))
|
||||
}
|
||||
require.NoError(t, os.Remove(probePath))
|
||||
return c
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,645 @@
|
||||
# SeaweedFS FUSE POSIX Compliance Testing
|
||||
|
||||
## Overview
|
||||
|
||||
This comprehensive test suite provides full POSIX compliance testing for SeaweedFS FUSE mounts. It includes basic POSIX operations, extended features, external test suite integration, and performance benchmarking to ensure SeaweedFS meets filesystem standards.
|
||||
|
||||
## 🎯 Key Features
|
||||
|
||||
### ✅ **Comprehensive Test Coverage**
|
||||
- **Basic POSIX Operations**: File/directory create, read, write, delete, rename
|
||||
- **Advanced Features**: Extended attributes, file locking, memory mapping *(see roadmap)*
|
||||
- **I/O Operations**: Synchronous/asynchronous I/O, direct I/O, vectored I/O *(see roadmap)*
|
||||
- **Error Handling**: Comprehensive error condition testing
|
||||
- **Concurrent Operations**: Multi-threaded stress testing
|
||||
- **External Integration**: pjdfstest, nfstest, FIO integration
|
||||
|
||||
> **📋 Implementation Status**: Some advanced features are currently skipped pending platform-specific implementations. See [`POSIX_IMPLEMENTATION_ROADMAP.md`](./POSIX_IMPLEMENTATION_ROADMAP.md) for a detailed implementation plan and current status.
|
||||
|
||||
### 📊 **Performance Analysis**
|
||||
- **Benchmarking**: Built-in performance benchmarks
|
||||
- **Profiling**: CPU and memory profiling support
|
||||
- **Coverage Analysis**: Code coverage reporting
|
||||
- **Stress Testing**: High-load concurrent operation testing
|
||||
|
||||
### 🔧 **External Tool Integration**
|
||||
- **pjdfstest**: Industry-standard POSIX filesystem test suite
|
||||
- **nfstest_posix**: Network filesystem POSIX API verification
|
||||
- **FIO**: Flexible I/O performance testing
|
||||
- **Custom Litmus Tests**: Focused edge case testing
|
||||
|
||||
## 📋 Test Categories
|
||||
|
||||
### 1. Basic POSIX Compliance (`posix_compliance_test.go`)
|
||||
|
||||
#### File Operations
|
||||
- ✅ File creation with O_CREAT, O_EXCL flags
|
||||
- ✅ File truncation and size management
|
||||
- ✅ File deletion and unlinking
|
||||
- ✅ Atomic rename operations
|
||||
- ✅ File permission management
|
||||
|
||||
#### Directory Operations
|
||||
- ✅ Directory creation and removal
|
||||
- ✅ Directory listing and traversal
|
||||
- ✅ Directory rename operations
|
||||
- ✅ Non-empty directory handling
|
||||
|
||||
#### Symlink Operations
|
||||
- ✅ Symbolic link creation and resolution
|
||||
- ✅ Broken symlink handling
|
||||
- ✅ Symlink target verification
|
||||
- ✅ Symlink permission handling
|
||||
|
||||
#### Permission Tests
|
||||
- ✅ File permission setting and verification
|
||||
- ✅ Directory permission enforcement
|
||||
- ✅ Permission inheritance
|
||||
- ✅ Access control validation
|
||||
|
||||
#### Timestamp Tests
|
||||
- ✅ Access time (atime) updates
|
||||
- ✅ Modification time (mtime) tracking
|
||||
- ✅ Change time (ctime) management
|
||||
- ✅ Timestamp precision and setting
|
||||
|
||||
#### I/O Operations
|
||||
- ✅ Read/write operations
|
||||
- ✅ File seeking and positioning
|
||||
- ✅ Append mode operations
|
||||
- ✅ Buffer management
|
||||
|
||||
#### File Descriptors
|
||||
- ✅ File descriptor duplication
|
||||
- ✅ File descriptor flags
|
||||
- ✅ Close-on-exec handling
|
||||
- ✅ File descriptor limits
|
||||
|
||||
#### Atomic Operations
|
||||
- ✅ Atomic file operations
|
||||
- ✅ Rename atomicity
|
||||
- ✅ Link/unlink atomicity
|
||||
|
||||
#### Concurrent Access
|
||||
- ✅ Multi-reader scenarios
|
||||
- ✅ Concurrent write handling
|
||||
- ✅ Reader-writer coordination
|
||||
- ✅ Race condition prevention
|
||||
|
||||
#### Error Handling
|
||||
- ✅ ENOENT (file not found) handling
|
||||
- ✅ EACCES (permission denied) handling
|
||||
- ✅ EBADF (bad file descriptor) handling
|
||||
- ✅ ENOTEMPTY (directory not empty) handling
|
||||
- ✅ Error code compliance
|
||||
|
||||
### 2. Extended POSIX Features (`posix_extended_test.go`)
|
||||
|
||||
#### Extended Attributes
|
||||
- ✅ Setting extended attributes (setxattr)
|
||||
- ✅ Getting extended attributes (getxattr)
|
||||
- ✅ Listing extended attributes (listxattr)
|
||||
- ✅ Removing extended attributes (removexattr)
|
||||
- ✅ Extended attribute namespaces
|
||||
|
||||
#### File Locking
|
||||
- ✅ Advisory locking (fcntl)
|
||||
- ✅ Exclusive locks (F_WRLCK)
|
||||
- ✅ Shared locks (F_RDLCK)
|
||||
- ✅ Lock conflict detection
|
||||
- ✅ Lock release and cleanup
|
||||
|
||||
#### Advanced I/O
|
||||
- ✅ Vectored I/O (readv/writev)
|
||||
- ✅ Positioned I/O (pread/pwrite)
|
||||
- ✅ Asynchronous I/O patterns
|
||||
- ✅ Scatter-gather operations
|
||||
|
||||
#### Sparse Files
|
||||
- ✅ Sparse file creation
|
||||
- ✅ Hole detection and handling
|
||||
- ✅ Sparse file efficiency
|
||||
- ✅ Seek beyond EOF
|
||||
|
||||
#### Large Files (>2GB)
|
||||
- ✅ Large file operations
|
||||
- ✅ 64-bit offset handling
|
||||
- ✅ Large file seeking
|
||||
- ✅ Large file truncation
|
||||
|
||||
#### Memory Mapping
|
||||
- ✅ File memory mapping (mmap)
|
||||
- ✅ Memory-mapped I/O
|
||||
- ✅ Memory synchronization (msync)
|
||||
- ✅ Memory unmapping (munmap)
|
||||
- ✅ Shared/private mappings
|
||||
|
||||
#### Direct I/O
|
||||
- ✅ Direct I/O operations (O_DIRECT)
|
||||
- ✅ Buffer alignment requirements
|
||||
- ✅ Direct I/O performance
|
||||
- ✅ Bypass buffer cache
|
||||
|
||||
#### File Preallocation
|
||||
- ✅ Space preallocation (fallocate)
|
||||
- ✅ File hole punching
|
||||
- ✅ Space reservation
|
||||
- ✅ Allocation efficiency
|
||||
|
||||
#### Zero-Copy Operations
|
||||
- ✅ Zero-copy file transfer (sendfile)
|
||||
- ✅ Efficient data movement
|
||||
- ✅ Cross-filesystem transfers
|
||||
|
||||
### 3. External Test Suite Integration (`posix_external_test.go`)
|
||||
|
||||
#### pjdfstest Integration
|
||||
- ✅ Comprehensive POSIX filesystem test suite
|
||||
- ✅ Industry-standard compliance verification
|
||||
- ✅ Automated test execution
|
||||
- ✅ Result analysis and reporting
|
||||
|
||||
#### nfstest_posix Integration
|
||||
- ✅ POSIX API verification
|
||||
- ✅ Network filesystem compliance
|
||||
- ✅ API behavior validation
|
||||
- ✅ Comprehensive coverage
|
||||
|
||||
#### Custom Litmus Tests
|
||||
- ✅ Atomic rename verification
|
||||
- ✅ Link count accuracy
|
||||
- ✅ Symlink cycle detection
|
||||
- ✅ Concurrent operation safety
|
||||
- ✅ Directory consistency
|
||||
|
||||
#### Stress Testing
|
||||
- ✅ High-frequency operations
|
||||
- ✅ Concurrent access patterns
|
||||
- ✅ Resource exhaustion scenarios
|
||||
- ✅ Error recovery testing
|
||||
|
||||
#### Edge Case Testing
|
||||
- ✅ Empty filenames
|
||||
- ✅ Very long filenames
|
||||
- ✅ Special characters
|
||||
- ✅ Deep directory nesting
|
||||
- ✅ Path length limits
|
||||
|
||||
## 🚀 Quick Start
|
||||
|
||||
### Prerequisites
|
||||
|
||||
```bash
|
||||
# Install required tools
|
||||
sudo apt-get install fuse # Linux
|
||||
brew install macfuse # macOS
|
||||
|
||||
# Install Go 1.21+
|
||||
go version
|
||||
|
||||
# Build SeaweedFS
|
||||
cd /path/to/seaweedfs
|
||||
make
|
||||
```
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```bash
|
||||
# Navigate to test directory
|
||||
cd test/fuse_integration
|
||||
|
||||
# Check prerequisites
|
||||
make -f posix_Makefile check-prereqs
|
||||
|
||||
# Run basic POSIX compliance tests
|
||||
make -f posix_Makefile test-posix-basic
|
||||
|
||||
# Run complete test suite
|
||||
make -f posix_Makefile test-posix-full
|
||||
|
||||
# Generate compliance report
|
||||
make -f posix_Makefile generate-report
|
||||
```
|
||||
|
||||
## 🔧 Advanced Usage
|
||||
|
||||
### Test Categories
|
||||
|
||||
#### Critical Tests Only
|
||||
```bash
|
||||
# Run only critical POSIX compliance tests (faster)
|
||||
make -f posix_Makefile test-posix-critical
|
||||
```
|
||||
|
||||
#### Extended Feature Tests
|
||||
```bash
|
||||
# Test advanced POSIX features
|
||||
make -f posix_Makefile test-posix-extended
|
||||
```
|
||||
|
||||
#### External Tool Integration
|
||||
```bash
|
||||
# Setup and run external test suites
|
||||
make -f posix_Makefile setup-external-tools
|
||||
make -f posix_Makefile test-posix-external
|
||||
```
|
||||
|
||||
#### Stress Testing
|
||||
```bash
|
||||
# Run stress tests for concurrent operations
|
||||
make -f posix_Makefile test-posix-stress
|
||||
```
|
||||
|
||||
### Performance Analysis
|
||||
|
||||
#### Benchmarking
|
||||
```bash
|
||||
# Run performance benchmarks
|
||||
make -f posix_Makefile benchmark-posix
|
||||
```
|
||||
|
||||
#### Profiling
|
||||
```bash
|
||||
# Profile CPU and memory usage
|
||||
make -f posix_Makefile profile-posix
|
||||
|
||||
# View CPU profile
|
||||
go tool pprof reports/posix.cpu.prof
|
||||
```
|
||||
|
||||
#### Coverage Analysis
|
||||
```bash
|
||||
# Generate test coverage report
|
||||
make -f posix_Makefile coverage-posix
|
||||
# View: reports/posix_coverage.html
|
||||
```
|
||||
|
||||
#### FIO Performance Testing
|
||||
```bash
|
||||
# Run FIO-based I/O performance tests
|
||||
make -f posix_Makefile test-fio-posix
|
||||
```
|
||||
|
||||
### External Test Suites
|
||||
|
||||
#### pjdfstest
|
||||
```bash
|
||||
# Setup and run pjdfstest
|
||||
make -f posix_Makefile setup-pjdfstest
|
||||
make -f posix_Makefile test-pjdfstest
|
||||
```
|
||||
|
||||
#### nfstest_posix
|
||||
```bash
|
||||
# Install and run nfstest_posix
|
||||
make -f posix_Makefile setup-nfstest
|
||||
make -f posix_Makefile test-nfstest-posix
|
||||
```
|
||||
|
||||
## 📊 Understanding Results
|
||||
|
||||
### Test Status Indicators
|
||||
|
||||
- ✅ **PASS**: Test completed successfully
|
||||
- ❌ **FAIL**: Test failed - indicates non-compliance
|
||||
- ⚠️ **SKIP**: Test skipped - feature not supported
|
||||
- 🔄 **RETRY**: Test retried due to transient failure
|
||||
|
||||
### Report Formats
|
||||
|
||||
#### HTML Report (`reports/posix_compliance_report.html`)
|
||||
- Interactive web-based report
|
||||
- Test category breakdown
|
||||
- Detailed results with navigation
|
||||
- Visual status indicators
|
||||
|
||||
#### Text Summary (`reports/posix_compliance_summary.txt`)
|
||||
- Concise text-based summary
|
||||
- Key findings and recommendations
|
||||
- Command-line friendly format
|
||||
|
||||
#### JSON Report (`reports/posix_compliance_report.json`)
|
||||
- Machine-readable results
|
||||
- Integration with CI/CD pipelines
|
||||
- Automated result processing
|
||||
|
||||
### Log Files
|
||||
|
||||
- `posix_basic_results.log`: Basic POSIX test detailed output
|
||||
- `posix_extended_results.log`: Extended feature test output
|
||||
- `posix_external_results.log`: External test suite output
|
||||
- `posix_benchmark_results.log`: Performance benchmark results
|
||||
|
||||
## 🐛 Debugging Failed Tests
|
||||
|
||||
### Common Issues
|
||||
|
||||
#### Permission Denied Errors
|
||||
```bash
|
||||
# Ensure proper FUSE permissions
|
||||
sudo usermod -a -G fuse $USER
|
||||
# Re-login or run: newgrp fuse
|
||||
|
||||
# Mount with -allowOthers if other users need access (test config MountOptions)
|
||||
```
|
||||
|
||||
#### Extended Attributes Not Supported
|
||||
```bash
|
||||
# Check if xattr is supported by underlying filesystem
|
||||
getfattr --version
|
||||
# May need to skip xattr tests on certain filesystems
|
||||
```
|
||||
|
||||
#### File Locking Issues
|
||||
```bash
|
||||
# Verify file locking support
|
||||
# Some network filesystems may not support full locking
|
||||
```
|
||||
|
||||
#### Memory Issues
|
||||
```bash
|
||||
# Increase available memory for tests
|
||||
# Large file tests may require significant RAM
|
||||
```
|
||||
|
||||
### Debug Mode
|
||||
|
||||
```bash
|
||||
# Test targets already run go test -v and keep logs under reports/
|
||||
make -f posix_Makefile test-posix-basic
|
||||
|
||||
# Run against an existing external mount instead of a fresh cluster
|
||||
make -f posix_Makefile test-posix-basic TEST_MOUNT_POINT=/path/to/mount
|
||||
|
||||
# Extend the 45-minute per-suite timeout
|
||||
make -f posix_Makefile test-posix-full TEST_TIMEOUT=90m
|
||||
```
|
||||
|
||||
### Manual Test Execution
|
||||
|
||||
```bash
|
||||
# Run specific test
|
||||
cd test/fuse_integration
|
||||
go test -v -run 'TestPOSIXCompliance/FileOperations' .
|
||||
|
||||
# Run with custom timeout
|
||||
go test -v -timeout 30m -run TestPOSIXExtended .
|
||||
```
|
||||
|
||||
## 🔄 CI/CD Integration
|
||||
|
||||
### GitHub Actions
|
||||
|
||||
```yaml
|
||||
name: POSIX Compliance Tests
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
posix-compliance:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: '1.21'
|
||||
|
||||
- name: Install FUSE
|
||||
run: sudo apt-get update && sudo apt-get install -y fuse
|
||||
|
||||
- name: Build SeaweedFS
|
||||
run: make
|
||||
|
||||
- name: Run POSIX Compliance Tests
|
||||
run: |
|
||||
cd test/fuse_integration
|
||||
make -f posix_Makefile ci-posix-tests
|
||||
|
||||
- name: Upload Test Results
|
||||
uses: actions/upload-artifact@v4
|
||||
if: always()
|
||||
with:
|
||||
name: posix-test-results
|
||||
path: test/fuse_integration/reports/
|
||||
```
|
||||
|
||||
### Jenkins Pipeline
|
||||
|
||||
```groovy
|
||||
pipeline {
|
||||
agent any
|
||||
stages {
|
||||
stage('Setup') {
|
||||
steps {
|
||||
sh 'make'
|
||||
}
|
||||
}
|
||||
stage('POSIX Compliance') {
|
||||
steps {
|
||||
dir('test/fuse_integration') {
|
||||
sh 'make -f posix_Makefile ci-posix-tests'
|
||||
}
|
||||
}
|
||||
post {
|
||||
always {
|
||||
archiveArtifacts 'test/fuse_integration/reports/**'
|
||||
publishHTML([
|
||||
allowMissing: false,
|
||||
alwaysLinkToLastBuild: true,
|
||||
keepAll: true,
|
||||
reportDir: 'test/fuse_integration/reports',
|
||||
reportFiles: 'posix_compliance_report.html',
|
||||
reportName: 'POSIX Compliance Report'
|
||||
])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 🎯 POSIX Compliance Checklist
|
||||
|
||||
### Critical POSIX Features
|
||||
- [ ] File creation, reading, writing, deletion
|
||||
- [ ] Directory operations (create, remove, list)
|
||||
- [ ] File permissions and ownership
|
||||
- [ ] Symbolic links
|
||||
- [ ] Hard links
|
||||
- [ ] File timestamps
|
||||
- [ ] Error handling compliance
|
||||
- [ ] Atomic operations
|
||||
|
||||
### Advanced POSIX Features
|
||||
- [ ] Extended attributes
|
||||
- [ ] File locking (advisory)
|
||||
- [ ] Memory-mapped I/O
|
||||
- [ ] Vectored I/O (readv/writev)
|
||||
- [ ] Positioned I/O (pread/pwrite)
|
||||
- [ ] Direct I/O
|
||||
- [ ] File preallocation
|
||||
- [ ] Sparse files
|
||||
|
||||
### Performance Requirements
|
||||
- [ ] Reasonable performance under load
|
||||
- [ ] Concurrent access handling
|
||||
- [ ] Memory usage optimization
|
||||
- [ ] CPU efficiency
|
||||
- [ ] I/O throughput
|
||||
|
||||
### Reliability Requirements
|
||||
- [ ] Data consistency
|
||||
- [ ] Error recovery
|
||||
- [ ] Race condition prevention
|
||||
- [ ] Resource cleanup
|
||||
- [ ] Crash recovery
|
||||
|
||||
## 📈 Performance Benchmarks
|
||||
|
||||
### Expected Performance Characteristics
|
||||
|
||||
| Operation | Expected Range | Notes |
|
||||
|-----------|----------------|-------|
|
||||
| File Create | 1000-10000 ops/sec | Depends on chunk size |
|
||||
| File Read | 100-1000 MB/sec | Network limited |
|
||||
| File Write | 50-500 MB/sec | Depends on replication |
|
||||
| Directory Ops | 500-5000 ops/sec | Metadata operations |
|
||||
| Concurrent Ops | Scales with cores | Up to hardware limits |
|
||||
|
||||
### Benchmarking Commands
|
||||
|
||||
```bash
|
||||
# Basic I/O performance
|
||||
make -f posix_Makefile benchmark-posix
|
||||
|
||||
# FIO comprehensive testing
|
||||
make -f posix_Makefile test-fio-posix
|
||||
|
||||
# Custom benchmark with specific parameters
|
||||
go test -v -bench=. -benchtime=30s posix_compliance_test.go
|
||||
```
|
||||
|
||||
## 🔍 Troubleshooting
|
||||
|
||||
### Common Test Failures
|
||||
|
||||
#### 1. Mount Permission Issues
|
||||
```bash
|
||||
# Error: Permission denied mounting
|
||||
# Solution: Check FUSE permissions — add the user to the fuse group and
|
||||
# re-login rather than widening permissions on the device node
|
||||
sudo usermod -a -G fuse $USER
|
||||
```
|
||||
|
||||
#### 2. Extended Attribute Tests Fail
|
||||
```bash
|
||||
# Error: Operation not supported (ENOTSUP)
|
||||
# Solution: Check filesystem xattr support
|
||||
mount | grep $(df . | tail -1 | awk '{print $1}')
|
||||
# May need to remount with xattr support
|
||||
```
|
||||
|
||||
#### 3. File Locking Tests Fail
|
||||
```bash
|
||||
# Error: Function not implemented (ENOSYS)
|
||||
# Solution: Some network filesystems don't support locking
|
||||
# This may be expected behavior
|
||||
```
|
||||
|
||||
#### 4. Large File Tests Fail
|
||||
```bash
|
||||
# Error: No space left on device
|
||||
# Solution: Ensure sufficient disk space
|
||||
df -h /tmp # Check temp directory space
|
||||
# May need to clean up or use different temp directory
|
||||
```
|
||||
|
||||
#### 5. Performance Tests Timeout
|
||||
```bash
|
||||
# Error: Test timeout exceeded
|
||||
# Solution: Increase timeout or reduce test scope
|
||||
go test -timeout 60m -run TestPOSIXCompliance
|
||||
```
|
||||
|
||||
### Debug Information Collection
|
||||
|
||||
```bash
|
||||
# Collect system information
|
||||
uname -a > debug_info.txt
|
||||
mount >> debug_info.txt
|
||||
df -h >> debug_info.txt
|
||||
free -h >> debug_info.txt
|
||||
|
||||
# Collect SeaweedFS information
|
||||
./weed version >> debug_info.txt
|
||||
ps aux | grep weed >> debug_info.txt
|
||||
|
||||
# Collect test logs
|
||||
cp reports/*.log debug_logs/
|
||||
```
|
||||
|
||||
## 📚 Additional Resources
|
||||
|
||||
### POSIX Standards
|
||||
- [POSIX.1-2017 (IEEE Std 1003.1-2017)](https://pubs.opengroup.org/onlinepubs/9699919799/)
|
||||
- [Single UNIX Specification](https://www.opengroup.org/openbrand/register/)
|
||||
|
||||
### External Test Suites
|
||||
- [pjdfstest](https://github.com/pjd/pjdfstest) - Comprehensive POSIX filesystem tests
|
||||
- [nfstest](https://nfstest.readthedocs.io/) - Network filesystem testing
|
||||
- [FIO](https://fio.readthedocs.io/) - Flexible I/O tester
|
||||
|
||||
### SeaweedFS Documentation
|
||||
- [SeaweedFS Wiki](https://github.com/seaweedfs/seaweedfs/wiki)
|
||||
- [FUSE Mount Documentation](https://github.com/seaweedfs/seaweedfs/wiki/FUSE-Mount)
|
||||
- [Performance Tuning Guide](https://github.com/seaweedfs/seaweedfs/wiki/Optimization)
|
||||
|
||||
## 🤝 Contributing
|
||||
|
||||
### Adding New Tests
|
||||
|
||||
1. **Basic Tests**: Add to `posix_compliance_test.go`
|
||||
2. **Extended Tests**: Add to `posix_extended_test.go`
|
||||
3. **External Integration**: Add to `posix_external_test.go`
|
||||
|
||||
### Test Guidelines
|
||||
|
||||
```go
|
||||
func (s *POSIXTestSuite) TestNewFeature(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("SubTestName", func(t *testing.T) {
|
||||
// Test setup
|
||||
testFile := filepath.Join(mountPoint, "test.txt")
|
||||
|
||||
// Test execution
|
||||
err := someOperation(testFile)
|
||||
|
||||
// Assertions
|
||||
require.NoError(t, err)
|
||||
|
||||
// Cleanup (if needed)
|
||||
defer os.Remove(testFile)
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### Submitting Improvements
|
||||
|
||||
1. Fork the repository
|
||||
2. Create a feature branch
|
||||
3. Add comprehensive tests
|
||||
4. Ensure all existing tests pass
|
||||
5. Submit a pull request with detailed description
|
||||
|
||||
## 📞 Support
|
||||
|
||||
For questions, issues, or contributions:
|
||||
|
||||
- **GitHub Issues**: [SeaweedFS Issues](https://github.com/seaweedfs/seaweedfs/issues)
|
||||
- **Discussions**: [SeaweedFS Discussions](https://github.com/seaweedfs/seaweedfs/discussions)
|
||||
- **Documentation**: [SeaweedFS Wiki](https://github.com/seaweedfs/seaweedfs/wiki)
|
||||
|
||||
---
|
||||
|
||||
*This comprehensive POSIX compliance test suite ensures SeaweedFS FUSE mounts meet industry standards for filesystem behavior, performance, and reliability.*
|
||||
@@ -0,0 +1,272 @@
|
||||
# POSIX Compliance Implementation Roadmap
|
||||
|
||||
This document tracks the implementation status of POSIX features in the SeaweedFS FUSE mount compliance test suite and provides a roadmap for completing comprehensive POSIX compliance testing.
|
||||
|
||||
## Overview
|
||||
|
||||
The platform-specific tests listed below are implemented; this document records the implementation plan that produced them, plus the one feature still pending.
|
||||
|
||||
## Current Status
|
||||
|
||||
### ✅ Implemented Features
|
||||
- Basic file operations (create, read, write, delete)
|
||||
- Directory operations (mkdir, rmdir, rename)
|
||||
- File permissions and ownership
|
||||
- Symbolic and hard links
|
||||
- Basic I/O operations (seek, append, positioned I/O)
|
||||
- File descriptors and atomic operations
|
||||
- Concurrent access patterns
|
||||
- Error handling compliance
|
||||
- Timestamp operations
|
||||
|
||||
### 🚧 Partially Implemented
|
||||
- Cross-platform compatibility (basic implementation with platform-specific access time handling)
|
||||
- File sealing (still skipped; Linux-specific, pending)
|
||||
|
||||
### ✅ Implemented Platform-Specific Features
|
||||
|
||||
#### 1. Extended Attributes (xattr)
|
||||
**Priority: High**
|
||||
**Platforms: Linux, macOS, FreeBSD**
|
||||
|
||||
**Status:** Implemented
|
||||
- `TestExtendedAttributes/SetExtendedAttribute`
|
||||
- `TestExtendedAttributes/ListExtendedAttributes`
|
||||
- `TestExtendedAttributes/RemoveExtendedAttribute`
|
||||
|
||||
**Implementation Plan:**
|
||||
```go
|
||||
// Linux implementation
|
||||
//go:build linux
|
||||
func setXattr(path, name string, value []byte) error {
|
||||
return syscall.Setxattr(path, name, value, 0)
|
||||
}
|
||||
|
||||
// macOS implementation
|
||||
//go:build darwin
|
||||
func setXattr(path, name string, value []byte) error {
|
||||
return syscall.Setxattr(path, name, value, 0, 0)
|
||||
}
|
||||
```
|
||||
|
||||
**Required Files:**
|
||||
- `xattr_linux.go` - Linux syscall implementations
|
||||
- `xattr_darwin.go` - macOS syscall implementations
|
||||
- `xattr_freebsd.go` - FreeBSD syscall implementations
|
||||
- `xattr_unsupported.go` - Fallback for unsupported platforms
|
||||
|
||||
#### 2. Memory Mapping (mmap)
|
||||
**Priority: High**
|
||||
**Platforms: All POSIX systems**
|
||||
|
||||
**Status:** Implemented
|
||||
- `TestMemoryMapping/MemoryMappedRead`
|
||||
- `TestMemoryMapping/MemoryMappedWrite`
|
||||
|
||||
**Implementation Plan:**
|
||||
```go
|
||||
func testMmap(t *testing.T, filePath string) {
|
||||
fd, err := syscall.Open(filePath, syscall.O_RDWR, 0)
|
||||
require.NoError(t, err)
|
||||
defer syscall.Close(fd)
|
||||
|
||||
// Platform-specific mmap implementation
|
||||
data, err := syscall.Mmap(fd, 0, size, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
|
||||
require.NoError(t, err)
|
||||
defer syscall.Munmap(data)
|
||||
}
|
||||
```
|
||||
|
||||
**Required Files:**
|
||||
- `mmap_unix.go` - Unix-like systems implementation
|
||||
- `mmap_windows.go` - Windows implementation (if needed)
|
||||
|
||||
#### 3. Vectored I/O (readv/writev)
|
||||
**Priority: Medium**
|
||||
**Platforms: All POSIX systems**
|
||||
|
||||
**Status:** Implemented
|
||||
- `TestAdvancedIO/VectoredIO`
|
||||
|
||||
**Implementation Plan:**
|
||||
```go
|
||||
func testVectoredIO(t *testing.T, filePath string) {
|
||||
// Use syscall.Syscall for readv/writev
|
||||
// Linux: SYS_READV, SYS_WRITEV
|
||||
// Other platforms: platform-specific syscall numbers
|
||||
}
|
||||
```
|
||||
|
||||
#### 4. Direct I/O
|
||||
**Priority: Medium**
|
||||
**Platforms: Linux, some Unix variants**
|
||||
|
||||
**Status:** Implemented
|
||||
- `TestDirectIO/DirectIO`
|
||||
|
||||
**Implementation Plan:**
|
||||
```go
|
||||
//go:build linux
|
||||
func testDirectIO(t *testing.T, filePath string) {
|
||||
fd, err := syscall.Open(filePath, syscall.O_RDWR|syscall.O_DIRECT, 0)
|
||||
// Test with aligned buffers
|
||||
}
|
||||
```
|
||||
|
||||
#### 5. File Preallocation (fallocate)
|
||||
**Priority: Medium**
|
||||
**Platforms: Linux, some Unix variants**
|
||||
|
||||
**Status:** Implemented
|
||||
- `TestFilePreallocation/Fallocate`
|
||||
|
||||
**Implementation Plan:**
|
||||
```go
|
||||
//go:build linux
|
||||
func testFallocate(t *testing.T, filePath string) {
|
||||
fd, err := syscall.Open(filePath, syscall.O_RDWR, 0)
|
||||
err = syscall.Syscall6(syscall.SYS_FALLOCATE, uintptr(fd), 0, 0, uintptr(size), 0, 0)
|
||||
}
|
||||
```
|
||||
|
||||
#### 6. Zero-Copy Transfer (sendfile)
|
||||
**Priority: Low**
|
||||
**Platforms: Linux, FreeBSD, some Unix variants**
|
||||
|
||||
**Status:** Implemented
|
||||
- `TestZeroCopyTransfer/Sendfile`
|
||||
|
||||
**Implementation Plan:**
|
||||
```go
|
||||
//go:build linux
|
||||
func testSendfile(t *testing.T, srcPath, dstPath string) {
|
||||
// Use syscall.Syscall for sendfile
|
||||
// Platform-specific implementations
|
||||
}
|
||||
```
|
||||
|
||||
#### 7. File Sealing (Linux-specific)
|
||||
**Priority: Low**
|
||||
**Platforms: Linux only**
|
||||
|
||||
**Current Status:** File sealing test is still skipped
|
||||
- `TestFileSealing`
|
||||
|
||||
**Implementation Plan:**
|
||||
```go
|
||||
//go:build linux
|
||||
func testFileSealing(t *testing.T) {
|
||||
// Use fcntl with F_ADD_SEALS, F_GET_SEALS
|
||||
// Test various seal types: F_SEAL_WRITE, F_SEAL_SHRINK, etc.
|
||||
}
|
||||
```
|
||||
|
||||
## Implementation Strategy
|
||||
|
||||
### Phase 1: Core Features (High Priority)
|
||||
1. **Extended Attributes** - Essential for many applications
|
||||
2. **Memory Mapping** - Critical for performance-sensitive applications
|
||||
|
||||
### Phase 2: Advanced I/O (Medium Priority)
|
||||
3. **Vectored I/O** - Important for efficient bulk operations
|
||||
4. **Direct I/O** - Needed for database and high-performance applications
|
||||
5. **File Preallocation** - Important for preventing fragmentation
|
||||
|
||||
### Phase 3: Specialized Features (Low Priority)
|
||||
6. **Zero-Copy Transfer** - Performance optimization feature
|
||||
7. **File Sealing** - Security feature, Linux-specific
|
||||
|
||||
## Platform Support Matrix
|
||||
|
||||
| Feature | Linux | macOS | FreeBSD | Windows | Notes |
|
||||
|---------|-------|-------|---------|---------|-------|
|
||||
| Extended Attributes | ✅ | ✅ | ✅ | ❌ | Different syscall signatures |
|
||||
| Memory Mapping | ✅ | ✅ | ✅ | ✅ | Standard POSIX |
|
||||
| Vectored I/O | ✅ | ✅ | ✅ | ❌ | readv/writev syscalls |
|
||||
| Direct I/O | ✅ | ❌ | ✅ | ❌ | O_DIRECT flag |
|
||||
| fallocate | ✅ | ❌ | ❌ | ❌ | Linux-specific |
|
||||
| sendfile | ✅ | ❌ | ✅ | ❌ | Platform-specific |
|
||||
| File Sealing | ✅ | ❌ | ❌ | ❌ | Linux-only |
|
||||
|
||||
## Development Guidelines
|
||||
|
||||
### 1. Platform-Specific Implementation Pattern
|
||||
```go
|
||||
// feature_linux.go
|
||||
//go:build linux
|
||||
package fuse
|
||||
func platformSpecificFunction() { /* Linux implementation */ }
|
||||
|
||||
// feature_darwin.go
|
||||
//go:build darwin
|
||||
package fuse
|
||||
func platformSpecificFunction() { /* macOS implementation */ }
|
||||
|
||||
// feature_unsupported.go
|
||||
//go:build !linux && !darwin
|
||||
package fuse
|
||||
func platformSpecificFunction() { /* Skip or error */ }
|
||||
```
|
||||
|
||||
### 2. Test Structure Pattern
|
||||
```go
|
||||
func (s *POSIXExtendedTestSuite) TestFeature(t *testing.T) {
|
||||
if !isFeatureSupported() {
|
||||
t.Skip("Feature not supported on this platform")
|
||||
return
|
||||
}
|
||||
|
||||
t.Run("FeatureTest", func(t *testing.T) {
|
||||
// Actual test implementation
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Error Handling
|
||||
- Use platform-specific error checking
|
||||
- Provide meaningful error messages
|
||||
- Gracefully handle unsupported features
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
### 1. Continuous Integration
|
||||
- Run tests on multiple platforms (Linux, macOS)
|
||||
- Use build tags to enable/disable platform-specific tests
|
||||
- Ensure graceful degradation on unsupported platforms
|
||||
|
||||
### 2. Feature Detection
|
||||
- Implement runtime feature detection where possible
|
||||
- Skip tests gracefully when features are unavailable
|
||||
- Log clear messages about skipped functionality
|
||||
|
||||
### 3. Documentation
|
||||
- Document platform-specific behavior
|
||||
- Provide examples for each implemented feature
|
||||
- Maintain compatibility matrices
|
||||
|
||||
## Contributing
|
||||
|
||||
When implementing these features:
|
||||
|
||||
1. **Start with high-priority items** (Extended Attributes, Memory Mapping)
|
||||
2. **Follow the platform-specific pattern** outlined above
|
||||
3. **Add comprehensive tests** for each feature
|
||||
4. **Update this roadmap** as features are implemented
|
||||
5. **Document any platform-specific quirks** or limitations
|
||||
|
||||
## Future Enhancements
|
||||
|
||||
Beyond the current roadmap, consider:
|
||||
- **File locking** (flock, fcntl locks)
|
||||
- **Asynchronous I/O** (aio_read, aio_write)
|
||||
- **File change notifications** (inotify, kqueue)
|
||||
- **POSIX ACLs** (Access Control Lists)
|
||||
- **Sparse file operations**
|
||||
- **File hole punching**
|
||||
|
||||
## References
|
||||
|
||||
- [POSIX.1-2017 Standard](https://pubs.opengroup.org/onlinepubs/9699919799/)
|
||||
- [Linux Programmer's Manual](https://man7.org/linux/man-pages/)
|
||||
- [FreeBSD System Calls](https://www.freebsd.org/cgi/man.cgi)
|
||||
- [macOS System Calls](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/)
|
||||
@@ -0,0 +1,12 @@
|
||||
//go:build linux
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// getAtimeNano returns the access time in nanoseconds for Linux systems
|
||||
func getAtimeNano(stat *syscall.Stat_t) int64 {
|
||||
return stat.Atim.Sec*1e9 + stat.Atim.Nsec
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
//go:build !linux && !windows
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// getAtimeNano returns the access time in nanoseconds for non-Linux systems (macOS, BSD, etc.)
|
||||
func getAtimeNano(stat *syscall.Stat_t) int64 {
|
||||
return stat.Atimespec.Sec*1e9 + stat.Atimespec.Nsec
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
//go:build windows
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// getAtimeNano returns the access time in nanoseconds for Windows systems
|
||||
func getAtimeNano(stat *syscall.Win32FileAttributeData) int64 {
|
||||
return stat.LastAccessTime.Nanoseconds()
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
//go:build darwin
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// Direct I/O support for macOS
|
||||
|
||||
const (
|
||||
// macOS doesn't have O_DIRECT, but we can use fcntl with F_NOCACHE
|
||||
F_NOCACHE = 48
|
||||
)
|
||||
|
||||
func openDirectIO(path string, flags int, mode uint32) (int, error) {
|
||||
// Open file normally first
|
||||
fd, err := syscall.Open(path, flags, mode)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
// Set F_NOCACHE to bypass buffer cache (similar to O_DIRECT)
|
||||
_, _, errno := syscall.Syscall(syscall.SYS_FCNTL,
|
||||
uintptr(fd),
|
||||
F_NOCACHE,
|
||||
1) // enable
|
||||
|
||||
if errno != 0 {
|
||||
syscall.Close(fd)
|
||||
return -1, errno
|
||||
}
|
||||
|
||||
return fd, nil
|
||||
}
|
||||
|
||||
func isDirectIOSupported() bool {
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
//go:build linux
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// Direct I/O support for Linux
|
||||
|
||||
const (
|
||||
O_DIRECT = unix.O_DIRECT
|
||||
)
|
||||
|
||||
func openDirectIO(path string, flags int, mode uint32) (int, error) {
|
||||
return syscall.Open(path, flags|O_DIRECT, mode)
|
||||
}
|
||||
|
||||
func isDirectIOSupported() bool {
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//go:build !linux && !darwin
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// Direct I/O support for unsupported platforms
|
||||
|
||||
var ErrDirectIONotSupported = errors.New("direct I/O not supported on this platform")
|
||||
|
||||
const (
|
||||
O_DIRECT = 0x4000 // Dummy flag for compatibility
|
||||
)
|
||||
|
||||
func openDirectIO(path string, flags int, mode uint32) (int, error) {
|
||||
// Fall back to regular open
|
||||
fd, err := syscall.Open(path, flags, mode)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
return int(fd), nil
|
||||
}
|
||||
|
||||
func isDirectIOSupported() bool {
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
//go:build darwin
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// File allocation support for macOS using fcntl
|
||||
|
||||
const (
|
||||
// macOS doesn't have fallocate, but we can use fcntl with F_PREALLOCATE
|
||||
F_ALLOCATECONTIG = 0x02
|
||||
F_ALLOCATEALL = 0x04
|
||||
|
||||
// Dummy flags for compatibility
|
||||
FALLOC_FL_KEEP_SIZE = 0x01
|
||||
FALLOC_FL_PUNCH_HOLE = 0x02
|
||||
FALLOC_FL_NO_HIDE_STALE = 0x04
|
||||
FALLOC_FL_COLLAPSE_RANGE = 0x08
|
||||
FALLOC_FL_ZERO_RANGE = 0x10
|
||||
FALLOC_FL_INSERT_RANGE = 0x20
|
||||
FALLOC_FL_UNSHARE_RANGE = 0x40
|
||||
)
|
||||
|
||||
// fstore_t structure for F_PREALLOCATE
|
||||
type fstore struct {
|
||||
flags uint32
|
||||
posmode int16
|
||||
offset int64
|
||||
length int64
|
||||
bytesalloc int64
|
||||
}
|
||||
|
||||
func fallocateFile(fd int, mode int, offset int64, length int64) error {
|
||||
// Check for unsupported modes on macOS
|
||||
unsupportedModes := FALLOC_FL_PUNCH_HOLE | FALLOC_FL_NO_HIDE_STALE | FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE
|
||||
if mode&unsupportedModes != 0 {
|
||||
return syscall.ENOTSUP // Operation not supported
|
||||
}
|
||||
|
||||
// On macOS, we use fcntl with F_PREALLOCATE
|
||||
store := fstore{
|
||||
flags: F_ALLOCATECONTIG,
|
||||
posmode: syscall.F_PEOFPOSMODE, // Allocate from EOF
|
||||
offset: 0,
|
||||
length: length,
|
||||
}
|
||||
|
||||
_, _, errno := syscall.Syscall(syscall.SYS_FCNTL,
|
||||
uintptr(fd),
|
||||
syscall.F_PREALLOCATE,
|
||||
uintptr(unsafe.Pointer(&store)))
|
||||
|
||||
if errno != 0 {
|
||||
// If contiguous allocation fails, try non-contiguous
|
||||
store.flags = F_ALLOCATEALL
|
||||
_, _, errno = syscall.Syscall(syscall.SYS_FCNTL,
|
||||
uintptr(fd),
|
||||
syscall.F_PREALLOCATE,
|
||||
uintptr(unsafe.Pointer(&store)))
|
||||
|
||||
if errno != 0 {
|
||||
return errno
|
||||
}
|
||||
}
|
||||
|
||||
// Set file size if not keeping size
|
||||
if mode&FALLOC_FL_KEEP_SIZE == 0 {
|
||||
return syscall.Ftruncate(fd, offset+length)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func isFallocateSupported() bool {
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
//go:build linux
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// File allocation support for Linux
|
||||
|
||||
const (
|
||||
// Fallocate flags
|
||||
FALLOC_FL_KEEP_SIZE = 0x01
|
||||
FALLOC_FL_PUNCH_HOLE = 0x02
|
||||
FALLOC_FL_NO_HIDE_STALE = 0x04
|
||||
FALLOC_FL_COLLAPSE_RANGE = 0x08
|
||||
FALLOC_FL_ZERO_RANGE = 0x10
|
||||
FALLOC_FL_INSERT_RANGE = 0x20
|
||||
FALLOC_FL_UNSHARE_RANGE = 0x40
|
||||
)
|
||||
|
||||
func fallocateFile(fd int, mode int, offset int64, length int64) error {
|
||||
_, _, errno := syscall.Syscall6(syscall.SYS_FALLOCATE,
|
||||
uintptr(fd),
|
||||
uintptr(mode),
|
||||
uintptr(offset),
|
||||
uintptr(length),
|
||||
0, 0)
|
||||
|
||||
if errno != 0 {
|
||||
return errno
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isFallocateSupported() bool {
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
//go:build !linux && !darwin
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// File allocation support for unsupported platforms
|
||||
|
||||
var ErrFallocateNotSupported = errors.New("fallocate not supported on this platform")
|
||||
|
||||
const (
|
||||
// Dummy flags for compatibility
|
||||
FALLOC_FL_KEEP_SIZE = 0x01
|
||||
FALLOC_FL_PUNCH_HOLE = 0x02
|
||||
FALLOC_FL_NO_HIDE_STALE = 0x04
|
||||
FALLOC_FL_COLLAPSE_RANGE = 0x08
|
||||
FALLOC_FL_ZERO_RANGE = 0x10
|
||||
FALLOC_FL_INSERT_RANGE = 0x20
|
||||
FALLOC_FL_UNSHARE_RANGE = 0x40
|
||||
)
|
||||
|
||||
func fallocateFile(fd int, mode int, offset int64, length int64) error {
|
||||
return ErrFallocateNotSupported
|
||||
}
|
||||
|
||||
func isFallocateSupported() bool {
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,496 @@
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// FuseTestFramework provides utilities for FUSE integration testing
|
||||
type FuseTestFramework struct {
|
||||
t *testing.T
|
||||
tempDir string
|
||||
mountPoint string
|
||||
dataDir string
|
||||
masterProcess *os.Process
|
||||
volumeProcess *os.Process
|
||||
filerProcess *os.Process
|
||||
mountProcess *os.Process
|
||||
masterAddr string
|
||||
volumeAddr string
|
||||
filerAddr string
|
||||
weedBinary string
|
||||
isSetup bool
|
||||
config *TestConfig
|
||||
externalMount bool
|
||||
}
|
||||
|
||||
// TestConfig holds configuration for FUSE tests
|
||||
type TestConfig struct {
|
||||
Collection string
|
||||
Replication string
|
||||
ChunkSizeMB int
|
||||
CacheSizeMB int
|
||||
NumVolumes int
|
||||
EnableDebug bool
|
||||
MountOptions []string
|
||||
SkipCleanup bool // for debugging failed tests
|
||||
}
|
||||
|
||||
// DefaultTestConfig returns a default configuration for FUSE tests
|
||||
func DefaultTestConfig() *TestConfig {
|
||||
return &TestConfig{
|
||||
Collection: "",
|
||||
Replication: "000",
|
||||
ChunkSizeMB: 4,
|
||||
CacheSizeMB: 100,
|
||||
NumVolumes: 3,
|
||||
EnableDebug: false,
|
||||
MountOptions: []string{},
|
||||
SkipCleanup: false,
|
||||
}
|
||||
}
|
||||
|
||||
// NewFuseTestFramework creates a new FUSE testing framework
|
||||
func NewFuseTestFramework(t *testing.T, config *TestConfig) *FuseTestFramework {
|
||||
if config == nil {
|
||||
config = DefaultTestConfig()
|
||||
}
|
||||
|
||||
tempDir, err := os.MkdirTemp("", "seaweedfs_fuse_test_")
|
||||
require.NoError(t, err)
|
||||
|
||||
return &FuseTestFramework{
|
||||
t: t,
|
||||
tempDir: tempDir,
|
||||
mountPoint: filepath.Join(tempDir, "mount"),
|
||||
dataDir: filepath.Join(tempDir, "data"),
|
||||
masterAddr: "127.0.0.1:19333",
|
||||
volumeAddr: "127.0.0.1:18080",
|
||||
filerAddr: "127.0.0.1:18888",
|
||||
weedBinary: findWeedBinary(),
|
||||
isSetup: false,
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
// Setup starts SeaweedFS cluster and mounts FUSE filesystem
|
||||
func (f *FuseTestFramework) Setup(config *TestConfig) error {
|
||||
if f.isSetup {
|
||||
return fmt.Errorf("framework already setup")
|
||||
}
|
||||
if config != nil {
|
||||
f.config = config
|
||||
}
|
||||
config = f.config
|
||||
|
||||
// Check if we should skip cluster setup and use existing mount
|
||||
if os.Getenv("TEST_SKIP_CLUSTER_SETUP") == "true" {
|
||||
// Use existing mount point from environment
|
||||
if existingMount := os.Getenv("TEST_MOUNT_POINT"); existingMount != "" {
|
||||
f.externalMount = true
|
||||
|
||||
// Verify mount point is accessible
|
||||
if _, err := os.Stat(existingMount); err != nil {
|
||||
return fmt.Errorf("existing mount point not accessible: %v", err)
|
||||
}
|
||||
|
||||
// A plain directory passes every check below; refuse to run the suite
|
||||
// against the local disk
|
||||
if !mountedAt(existingMount) {
|
||||
return fmt.Errorf("mount point %s is not a mounted filesystem", existingMount)
|
||||
}
|
||||
// So can a non-FUSE mount like tmpfs: a device boundary alone does
|
||||
// not make the results FUSE compliance
|
||||
if !fuseFilesystem(existingMount) {
|
||||
return fmt.Errorf("mount point %s is not a FUSE filesystem", existingMount)
|
||||
}
|
||||
|
||||
// Run inside a private work dir so test files do not pile up in
|
||||
// the shared mount root and cleanup can remove only what it made
|
||||
workDir, err := os.MkdirTemp(existingMount, ".fuse_test_")
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating work dir under existing mount: %v", err)
|
||||
}
|
||||
f.mountPoint = workDir
|
||||
f.t.Logf("Using existing mount point: %s (work dir %s)", existingMount, f.mountPoint)
|
||||
|
||||
// Test basic functionality
|
||||
testFile := filepath.Join(f.mountPoint, ".framework_test")
|
||||
if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil {
|
||||
return fmt.Errorf("mount point not writable: %v", err)
|
||||
}
|
||||
if err := os.Remove(testFile); err != nil {
|
||||
f.t.Logf("Warning: failed to cleanup test file: %v", err)
|
||||
}
|
||||
|
||||
// Verify this is actually a SeaweedFS mount by checking for SeaweedFS-specific behavior
|
||||
// Create a test file and verify it appears in the filer
|
||||
verifyFile := filepath.Join(f.mountPoint, ".seaweedfs_mount_verification")
|
||||
if err := os.WriteFile(verifyFile, []byte("SeaweedFS mount verification"), 0644); err != nil {
|
||||
return fmt.Errorf("mount point verification failed - cannot write: %v", err)
|
||||
}
|
||||
|
||||
// Read it back to ensure it's working
|
||||
if data, err := os.ReadFile(verifyFile); err != nil {
|
||||
return fmt.Errorf("mount point verification failed - cannot read: %v", err)
|
||||
} else if string(data) != "SeaweedFS mount verification" {
|
||||
return fmt.Errorf("mount point verification failed - data mismatch")
|
||||
}
|
||||
|
||||
// Clean up verification file
|
||||
if err := os.Remove(verifyFile); err != nil {
|
||||
f.t.Logf("Warning: failed to cleanup verification file: %v", err)
|
||||
}
|
||||
|
||||
f.t.Logf("✅ SeaweedFS mount point verified and working: %s", f.mountPoint)
|
||||
|
||||
f.isSetup = true
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Create directories for full cluster setup
|
||||
dirs := []string{f.mountPoint, f.dataDir}
|
||||
for _, dir := range dirs {
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create directory %s: %v", dir, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Start master
|
||||
if err := f.startMaster(config); err != nil {
|
||||
return fmt.Errorf("failed to start master: %v", err)
|
||||
}
|
||||
|
||||
// Wait for master to be ready
|
||||
if err := f.waitForService(f.masterAddr, 30*time.Second); err != nil {
|
||||
return fmt.Errorf("master not ready: %v", err)
|
||||
}
|
||||
|
||||
// Start volume servers
|
||||
if err := f.startVolumeServers(config); err != nil {
|
||||
return fmt.Errorf("failed to start volume servers: %v", err)
|
||||
}
|
||||
|
||||
// Wait for volume server to be ready
|
||||
if err := f.waitForService(f.volumeAddr, 30*time.Second); err != nil {
|
||||
return fmt.Errorf("volume server not ready: %v", err)
|
||||
}
|
||||
|
||||
// Start filer
|
||||
if err := f.startFiler(config); err != nil {
|
||||
return fmt.Errorf("failed to start filer: %v", err)
|
||||
}
|
||||
|
||||
// Wait for filer to be ready
|
||||
if err := f.waitForService(f.filerAddr, 30*time.Second); err != nil {
|
||||
return fmt.Errorf("filer not ready: %v", err)
|
||||
}
|
||||
|
||||
// Skip rather than fail on a host with no FUSE device to mount through
|
||||
if !fuseAvailable() {
|
||||
f.t.Skip("no FUSE device node (/dev/fuse, /dev/macfuse0, /dev/osxfuse0)")
|
||||
}
|
||||
|
||||
// Mount FUSE filesystem
|
||||
if err := f.mountFuse(config); err != nil {
|
||||
return fmt.Errorf("failed to mount FUSE: %v", err)
|
||||
}
|
||||
|
||||
// Wait for mount to be ready
|
||||
if err := f.waitForMount(30 * time.Second); err != nil {
|
||||
return fmt.Errorf("FUSE mount not ready: %v", err)
|
||||
}
|
||||
|
||||
f.isSetup = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// Cleanup stops all processes and removes temporary files
|
||||
func (f *FuseTestFramework) Cleanup() {
|
||||
// An external mount needs no teardown; only this run's work dir is ours.
|
||||
// A TEST_SKIP_CLUSTER_SETUP that fell through to a self-started cluster
|
||||
// still owns its processes and must not skip stopping them.
|
||||
if f.externalMount {
|
||||
if !f.config.SkipCleanup {
|
||||
os.RemoveAll(f.mountPoint)
|
||||
os.RemoveAll(f.tempDir)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if f.mountProcess != nil {
|
||||
f.unmountFuse()
|
||||
}
|
||||
|
||||
// Stop processes in reverse order
|
||||
processes := []*os.Process{f.mountProcess, f.filerProcess, f.volumeProcess, f.masterProcess}
|
||||
for _, proc := range processes {
|
||||
if proc != nil {
|
||||
proc.Signal(syscall.SIGTERM)
|
||||
proc.Wait()
|
||||
}
|
||||
}
|
||||
|
||||
// Remove temp directory
|
||||
if !f.config.SkipCleanup {
|
||||
os.RemoveAll(f.tempDir)
|
||||
}
|
||||
}
|
||||
|
||||
// GetMountPoint returns the FUSE mount point path
|
||||
func (f *FuseTestFramework) GetMountPoint() string {
|
||||
return f.mountPoint
|
||||
}
|
||||
|
||||
// GetFilerAddr returns the filer address
|
||||
func (f *FuseTestFramework) GetFilerAddr() string {
|
||||
return f.filerAddr
|
||||
}
|
||||
|
||||
// startMaster starts the SeaweedFS master server
|
||||
func (f *FuseTestFramework) startMaster(config *TestConfig) error {
|
||||
var args []string
|
||||
if config.EnableDebug {
|
||||
// -v is a global flag; before the subcommand or flag parsing dies
|
||||
args = append(args, "-v=4")
|
||||
}
|
||||
args = append(args,
|
||||
"master",
|
||||
"-ip=127.0.0.1",
|
||||
"-port=19333",
|
||||
"-mdir=" + filepath.Join(f.dataDir, "master"),
|
||||
"-raftBootstrap",
|
||||
)
|
||||
|
||||
cmd := exec.Command(f.weedBinary, args...)
|
||||
cmd.Dir = f.tempDir
|
||||
if err := cmd.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
f.masterProcess = cmd.Process
|
||||
return nil
|
||||
}
|
||||
|
||||
// startVolumeServers starts SeaweedFS volume servers
|
||||
func (f *FuseTestFramework) startVolumeServers(config *TestConfig) error {
|
||||
var args []string
|
||||
if config.EnableDebug {
|
||||
args = append(args, "-v=4")
|
||||
}
|
||||
args = append(args,
|
||||
"volume",
|
||||
"-mserver=" + f.masterAddr,
|
||||
"-ip=127.0.0.1",
|
||||
"-port=18080",
|
||||
"-dir=" + filepath.Join(f.dataDir, "volume"),
|
||||
fmt.Sprintf("-max=%d", config.NumVolumes),
|
||||
)
|
||||
|
||||
cmd := exec.Command(f.weedBinary, args...)
|
||||
cmd.Dir = f.tempDir
|
||||
if err := cmd.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
f.volumeProcess = cmd.Process
|
||||
return nil
|
||||
}
|
||||
|
||||
// startFiler starts the SeaweedFS filer server
|
||||
func (f *FuseTestFramework) startFiler(config *TestConfig) error {
|
||||
var args []string
|
||||
if config.EnableDebug {
|
||||
args = append(args, "-v=4")
|
||||
}
|
||||
args = append(args,
|
||||
"filer",
|
||||
"-master=" + f.masterAddr,
|
||||
"-ip=127.0.0.1",
|
||||
"-port=18888",
|
||||
)
|
||||
|
||||
cmd := exec.Command(f.weedBinary, args...)
|
||||
cmd.Dir = f.tempDir
|
||||
if err := cmd.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
f.filerProcess = cmd.Process
|
||||
return nil
|
||||
}
|
||||
|
||||
// mountFuse mounts the SeaweedFS FUSE filesystem
|
||||
func (f *FuseTestFramework) mountFuse(config *TestConfig) error {
|
||||
var args []string
|
||||
if config.EnableDebug {
|
||||
args = append(args, "-v=4")
|
||||
}
|
||||
args = append(args,
|
||||
"mount",
|
||||
"-filer=" + f.filerAddr,
|
||||
"-dir=" + f.mountPoint,
|
||||
"-filer.path=/",
|
||||
"-dirAutoCreate",
|
||||
)
|
||||
|
||||
if config.Collection != "" {
|
||||
args = append(args, "-collection="+config.Collection)
|
||||
}
|
||||
if config.Replication != "" {
|
||||
args = append(args, "-replication="+config.Replication)
|
||||
}
|
||||
if config.ChunkSizeMB > 0 {
|
||||
args = append(args, fmt.Sprintf("-chunkSizeLimitMB=%d", config.ChunkSizeMB))
|
||||
}
|
||||
if config.CacheSizeMB > 0 {
|
||||
args = append(args, fmt.Sprintf("-cacheSizeMB=%d", config.CacheSizeMB))
|
||||
}
|
||||
|
||||
args = append(args, config.MountOptions...)
|
||||
|
||||
cmd := exec.Command(f.weedBinary, args...)
|
||||
cmd.Dir = f.tempDir
|
||||
if err := cmd.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
f.mountProcess = cmd.Process
|
||||
return nil
|
||||
}
|
||||
|
||||
// unmountFuse unmounts the FUSE filesystem
|
||||
func (f *FuseTestFramework) unmountFuse() error {
|
||||
if f.mountProcess != nil {
|
||||
f.mountProcess.Signal(syscall.SIGTERM)
|
||||
f.mountProcess.Wait()
|
||||
f.mountProcess = nil
|
||||
}
|
||||
|
||||
// Also try system unmount as backup
|
||||
exec.Command("umount", f.mountPoint).Run()
|
||||
return nil
|
||||
}
|
||||
|
||||
// waitForService waits for a service to be available
|
||||
func (f *FuseTestFramework) waitForService(addr string, timeout time.Duration) error {
|
||||
deadline := time.Now().Add(timeout)
|
||||
for time.Now().Before(deadline) {
|
||||
conn, err := net.DialTimeout("tcp", addr, 1*time.Second)
|
||||
if err == nil {
|
||||
conn.Close()
|
||||
return nil
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
return fmt.Errorf("service at %s not ready within timeout", addr)
|
||||
}
|
||||
|
||||
// waitForMount waits for the FUSE mount to be ready
|
||||
func (f *FuseTestFramework) waitForMount(timeout time.Duration) error {
|
||||
// The mount point is created before weed mount starts, so Stat/ReadDir
|
||||
// alone succeed on the plain directory. The device id only differs from
|
||||
// its parent's once a filesystem is actually mounted on it.
|
||||
parentDev, err := deviceID(filepath.Dir(f.mountPoint))
|
||||
if err != nil {
|
||||
return fmt.Errorf("stat mount parent: %w", err)
|
||||
}
|
||||
deadline := time.Now().Add(timeout)
|
||||
for time.Now().Before(deadline) {
|
||||
if dev, err := deviceID(f.mountPoint); err == nil && dev != parentDev {
|
||||
if _, err := os.ReadDir(f.mountPoint); err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
return fmt.Errorf("mount point not ready within timeout")
|
||||
}
|
||||
|
||||
// fuseAvailable reports whether the host has a FUSE device node to mount
|
||||
// through: /dev/fuse on Linux, /dev/macfuse* or /dev/osxfuse* on macOS.
|
||||
func fuseAvailable() bool {
|
||||
for _, dev := range []string{"/dev/fuse", "/dev/macfuse0", "/dev/osxfuse0"} {
|
||||
if _, err := os.Stat(dev); err == nil {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// findWeedBinary locates the weed binary. Candidates are resolved to
|
||||
// absolute paths because the started processes run with cmd.Dir set to the
|
||||
// framework's temp dir, where a relative path would not resolve.
|
||||
func findWeedBinary() string {
|
||||
if p, err := exec.LookPath("weed"); err == nil {
|
||||
return p
|
||||
}
|
||||
|
||||
candidates := []string{
|
||||
"../../weed/weed",
|
||||
"./weed",
|
||||
"../weed",
|
||||
}
|
||||
for _, candidate := range candidates {
|
||||
if info, err := os.Stat(candidate); err == nil && !info.IsDir() && info.Mode()&0111 != 0 {
|
||||
if abs, err := filepath.Abs(candidate); err == nil {
|
||||
return abs
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Default fallback
|
||||
return "weed"
|
||||
}
|
||||
|
||||
// Helper functions for test assertions
|
||||
|
||||
// AssertFileExists checks if a file exists in the mount point
|
||||
func (f *FuseTestFramework) AssertFileExists(relativePath string) {
|
||||
fullPath := filepath.Join(f.mountPoint, relativePath)
|
||||
_, err := os.Stat(fullPath)
|
||||
require.NoError(f.t, err, "file should exist: %s", relativePath)
|
||||
}
|
||||
|
||||
// AssertFileNotExists checks if a file does not exist in the mount point
|
||||
func (f *FuseTestFramework) AssertFileNotExists(relativePath string) {
|
||||
fullPath := filepath.Join(f.mountPoint, relativePath)
|
||||
_, err := os.Stat(fullPath)
|
||||
require.True(f.t, os.IsNotExist(err), "file should not exist: %s", relativePath)
|
||||
}
|
||||
|
||||
// AssertFileContent checks if a file has expected content
|
||||
func (f *FuseTestFramework) AssertFileContent(relativePath string, expectedContent []byte) {
|
||||
fullPath := filepath.Join(f.mountPoint, relativePath)
|
||||
actualContent, err := os.ReadFile(fullPath)
|
||||
require.NoError(f.t, err, "failed to read file: %s", relativePath)
|
||||
require.Equal(f.t, expectedContent, actualContent, "file content mismatch: %s", relativePath)
|
||||
}
|
||||
|
||||
// AssertFileMode checks if a file has expected permissions
|
||||
func (f *FuseTestFramework) AssertFileMode(relativePath string, expectedMode fs.FileMode) {
|
||||
fullPath := filepath.Join(f.mountPoint, relativePath)
|
||||
info, err := os.Stat(fullPath)
|
||||
require.NoError(f.t, err, "failed to stat file: %s", relativePath)
|
||||
require.Equal(f.t, expectedMode, info.Mode(), "file mode mismatch: %s", relativePath)
|
||||
}
|
||||
|
||||
// CreateTestFile creates a test file with specified content
|
||||
func (f *FuseTestFramework) CreateTestFile(relativePath string, content []byte) {
|
||||
fullPath := filepath.Join(f.mountPoint, relativePath)
|
||||
dir := filepath.Dir(fullPath)
|
||||
require.NoError(f.t, os.MkdirAll(dir, 0755), "failed to create directory: %s", dir)
|
||||
require.NoError(f.t, os.WriteFile(fullPath, content, 0644), "failed to create file: %s", relativePath)
|
||||
}
|
||||
|
||||
// CreateTestDir creates a test directory
|
||||
func (f *FuseTestFramework) CreateTestDir(relativePath string) {
|
||||
fullPath := filepath.Join(f.mountPoint, relativePath)
|
||||
require.NoError(f.t, os.MkdirAll(fullPath, 0755), "failed to create directory: %s", relativePath)
|
||||
}
|
||||
@@ -30,6 +30,7 @@ type FuseTestFramework struct {
|
||||
filerPort int
|
||||
weedBinary string
|
||||
isSetup bool
|
||||
config *TestConfig
|
||||
}
|
||||
|
||||
// TestConfig holds configuration for FUSE tests
|
||||
@@ -83,6 +84,7 @@ func NewFuseTestFramework(t *testing.T, config *TestConfig) *FuseTestFramework {
|
||||
filerAddr: fmt.Sprintf("127.0.0.1:%d", filerPort),
|
||||
weedBinary: findWeedBinary(),
|
||||
isSetup: false,
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,6 +134,10 @@ func (f *FuseTestFramework) Setup(config *TestConfig) error {
|
||||
if f.isSetup {
|
||||
return fmt.Errorf("framework already setup")
|
||||
}
|
||||
if config != nil {
|
||||
f.config = config
|
||||
}
|
||||
config = f.config
|
||||
|
||||
dirs := []string{f.mountPoint, f.logDir, f.dataDir}
|
||||
for _, dir := range dirs {
|
||||
@@ -182,7 +188,7 @@ func (f *FuseTestFramework) Cleanup() {
|
||||
|
||||
f.copyLogsForCI()
|
||||
|
||||
if !DefaultTestConfig().SkipCleanup {
|
||||
if !f.config.SkipCleanup {
|
||||
os.RemoveAll(f.tempDir)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
//go:build darwin || freebsd
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// fuseFilesystem reports whether path sits on a FUSE mount; macFUSE, osxfuse
|
||||
// and fusefs all carry "fuse" in the filesystem type name.
|
||||
func fuseFilesystem(path string) bool {
|
||||
var st syscall.Statfs_t
|
||||
if err := syscall.Statfs(path, &st); err != nil {
|
||||
return false
|
||||
}
|
||||
name := make([]byte, 0, len(st.Fstypename))
|
||||
for _, c := range st.Fstypename {
|
||||
if c == 0 {
|
||||
break
|
||||
}
|
||||
name = append(name, byte(c))
|
||||
}
|
||||
return strings.Contains(string(name), "fuse")
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
//go:build linux
|
||||
|
||||
package fuse
|
||||
|
||||
import "syscall"
|
||||
|
||||
// fuseSuperMagic is statfs f_type for fuse and fuseblk mounts.
|
||||
const fuseSuperMagic = 0x65735546
|
||||
|
||||
// fuseFilesystem reports whether path sits on a FUSE mount.
|
||||
func fuseFilesystem(path string) bool {
|
||||
var st syscall.Statfs_t
|
||||
if err := syscall.Statfs(path, &st); err != nil {
|
||||
return false
|
||||
}
|
||||
return st.Type == fuseSuperMagic
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
//go:build (unix && !linux && !darwin && !freebsd) || (!unix && !windows)
|
||||
|
||||
package fuse
|
||||
|
||||
// fuseFilesystem has no portable check on these platforms; the mount check
|
||||
// and write verification in Setup stay the only gates.
|
||||
func fuseFilesystem(path string) bool {
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
//go:build unix
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// Memory mapping support for Unix-like systems
|
||||
|
||||
func mmapFile(fd int, offset int64, length int, prot int, flags int) ([]byte, error) {
|
||||
return unix.Mmap(fd, offset, length, prot, flags)
|
||||
}
|
||||
|
||||
func munmapFile(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
return unix.Munmap(data)
|
||||
}
|
||||
|
||||
func msyncFile(data []byte, flags int) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
return unix.Msync(data, flags)
|
||||
}
|
||||
|
||||
func isMmapSupported() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Memory protection flags
|
||||
const (
|
||||
PROT_READ = syscall.PROT_READ
|
||||
PROT_WRITE = syscall.PROT_WRITE
|
||||
PROT_EXEC = syscall.PROT_EXEC
|
||||
PROT_NONE = syscall.PROT_NONE
|
||||
)
|
||||
|
||||
// Memory mapping flags
|
||||
const (
|
||||
MAP_SHARED = syscall.MAP_SHARED
|
||||
MAP_PRIVATE = syscall.MAP_PRIVATE
|
||||
MAP_ANONYMOUS = syscall.MAP_ANON
|
||||
)
|
||||
|
||||
// Memory sync flags
|
||||
const (
|
||||
MS_ASYNC = syscall.MS_ASYNC
|
||||
MS_SYNC = syscall.MS_SYNC
|
||||
MS_INVALIDATE = syscall.MS_INVALIDATE
|
||||
)
|
||||
@@ -0,0 +1,47 @@
|
||||
//go:build !unix
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Memory mapping support for unsupported platforms
|
||||
|
||||
var ErrMmapNotSupported = errors.New("memory mapping not supported on this platform")
|
||||
|
||||
func mmapFile(fd int, offset int64, length int, prot int, flags int) ([]byte, error) {
|
||||
return nil, ErrMmapNotSupported
|
||||
}
|
||||
|
||||
func munmapFile(data []byte) error {
|
||||
return ErrMmapNotSupported
|
||||
}
|
||||
|
||||
func msyncFile(data []byte, flags int) error {
|
||||
return ErrMmapNotSupported
|
||||
}
|
||||
|
||||
func isMmapSupported() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Dummy constants for unsupported platforms
|
||||
const (
|
||||
PROT_READ = 0x1
|
||||
PROT_WRITE = 0x2
|
||||
PROT_EXEC = 0x4
|
||||
PROT_NONE = 0x0
|
||||
)
|
||||
|
||||
const (
|
||||
MAP_SHARED = 0x01
|
||||
MAP_PRIVATE = 0x02
|
||||
MAP_ANONYMOUS = 0x20
|
||||
)
|
||||
|
||||
const (
|
||||
MS_ASYNC = 0x1
|
||||
MS_SYNC = 0x4
|
||||
MS_INVALIDATE = 0x2
|
||||
)
|
||||
@@ -0,0 +1,27 @@
|
||||
//go:build linux
|
||||
|
||||
package fuse
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMountedAt(t *testing.T) {
|
||||
for _, p := range []string{"/proc", "/proc/"} {
|
||||
if !mountedAt(p) {
|
||||
t.Errorf("mountedAt(%q) = false, want true", p)
|
||||
}
|
||||
}
|
||||
plain := t.TempDir()
|
||||
for _, p := range []string{plain, plain + "/"} {
|
||||
if mountedAt(p) {
|
||||
t.Errorf("mountedAt(%q) = true, want false", p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFuseFilesystem(t *testing.T) {
|
||||
for _, p := range []string{"/proc", t.TempDir()} {
|
||||
if fuseFilesystem(p) {
|
||||
t.Errorf("fuseFilesystem(%q) = true, want false", p)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//go:build unix
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// deviceID returns the device ID of the filesystem containing path.
|
||||
func deviceID(path string) (uint64, error) {
|
||||
var st syscall.Stat_t
|
||||
if err := syscall.Stat(path, &st); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return uint64(st.Dev), nil
|
||||
}
|
||||
|
||||
// mountedAt reports whether something is actually mounted at path: a plain
|
||||
// directory shares its parent's device, a mount point does not. The path is
|
||||
// cleaned first so a trailing slash does not make Dir return the mount itself.
|
||||
func mountedAt(path string) bool {
|
||||
path = filepath.Clean(path)
|
||||
dev, err := deviceID(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
parentDev, err := deviceID(filepath.Dir(path))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return dev != parentDev
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
//go:build !unix && !windows
|
||||
|
||||
package fuse
|
||||
|
||||
// FUSE mounts are unix-only: reporting one device keeps waitForMount from
|
||||
// mistaking the plain pre-mount directory for a live mount on platforms
|
||||
// where a mount can never appear.
|
||||
func deviceID(path string) (uint64, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// mountedAt cannot be detected off unix; report mounted so an external
|
||||
// TEST_MOUNT_POINT keeps its best-effort write verification.
|
||||
func mountedAt(path string) bool {
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
//go:build windows
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// deviceID has no portable meaning here: reporting one device keeps
|
||||
// waitForMount from mistaking the plain pre-mount directory for a live mount.
|
||||
func deviceID(path string) (uint64, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// mountedAt reports whether path is a WinFsp mount: a directory mount is a
|
||||
// reparse point and a drive mount is a volume root, while a plain directory
|
||||
// is neither.
|
||||
func mountedAt(path string) bool {
|
||||
abs, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if filepath.Dir(abs) == abs {
|
||||
return true
|
||||
}
|
||||
info, err := os.Lstat(abs)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
data, ok := info.Sys().(*syscall.Win32FileAttributeData)
|
||||
return ok && data.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0
|
||||
}
|
||||
|
||||
// fuseFilesystem is covered by the WinFsp checks in mountedAt: WinFsp is the
|
||||
// only FUSE provider on the platform.
|
||||
func fuseFilesystem(path string) bool {
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,531 @@
|
||||
# SeaweedFS POSIX Compliance Testing Makefile
|
||||
|
||||
SHELL := /bin/bash
|
||||
# go test is piped through tee everywhere; without pipefail a failing suite
|
||||
# still exits 0 because only tee's status propagates.
|
||||
.SHELLFLAGS := -o pipefail -c
|
||||
|
||||
# Configuration
|
||||
WEED_BINARY := $(shell which weed 2>/dev/null || echo "../../weed")
|
||||
GO_VERSION := 1.21
|
||||
TEST_TIMEOUT := 45m
|
||||
COVERAGE_FILE := posix_coverage.out
|
||||
REPORT_DIR := reports
|
||||
EXTERNAL_TOOLS_DIR := external_tools
|
||||
MOUNT_POINT ?= /tmp/seaweedfs_mount
|
||||
ifneq ($(TEST_MOUNT_POINT),)
|
||||
MOUNT_POINT := $(TEST_MOUNT_POINT)
|
||||
endif
|
||||
|
||||
# Go test command prefix for external mount points
|
||||
GO_TEST_PREFIX :=
|
||||
ifneq ($(TEST_MOUNT_POINT),)
|
||||
GO_TEST_PREFIX := TEST_MOUNT_POINT="$(TEST_MOUNT_POINT)" TEST_SKIP_CLUSTER_SETUP="true"
|
||||
endif
|
||||
|
||||
# Test categories
|
||||
POSIX_BASIC_TESTS := posix_compliance_test.go
|
||||
POSIX_EXTENDED_TESTS := posix_extended_test.go
|
||||
POSIX_EXTERNAL_TESTS := posix_external_test.go
|
||||
|
||||
# -f is not propagated through MAKEFLAGS, so recursive calls must name this
|
||||
# file explicitly or the sub-make reads the default Makefile instead.
|
||||
MAKE_SELF := $(MAKE) -f $(firstword $(MAKEFILE_LIST))
|
||||
|
||||
# Colors for output — bash echo does not expand \033, so bake the escapes in
|
||||
RED := $(shell printf '\033[31m')
|
||||
GREEN := $(shell printf '\033[32m')
|
||||
YELLOW := $(shell printf '\033[33m')
|
||||
BLUE := $(shell printf '\033[34m')
|
||||
MAGENTA := $(shell printf '\033[35m')
|
||||
CYAN := $(shell printf '\033[36m')
|
||||
WHITE := $(shell printf '\033[37m')
|
||||
RESET := $(shell printf '\033[0m')
|
||||
|
||||
.DEFAULT_GOAL := help
|
||||
|
||||
# Prerequisites checks
|
||||
check-binary:
|
||||
@if [ ! -f "$(WEED_BINARY)" ]; then \
|
||||
echo "$(RED)❌ SeaweedFS binary not found at $(WEED_BINARY)$(RESET)"; \
|
||||
echo " Please run 'make' in the root directory first"; \
|
||||
exit 1; \
|
||||
fi
|
||||
@echo "$(GREEN)[OK] SeaweedFS binary found at $(WEED_BINARY)$(RESET)"
|
||||
|
||||
check-fuse:
|
||||
@if command -v fusermount >/dev/null 2>&1; then \
|
||||
echo "$(GREEN)[OK] FUSE is installed (Linux)$(RESET)"; \
|
||||
elif command -v umount >/dev/null 2>&1 && [ "$$(uname)" = "Darwin" ]; then \
|
||||
echo "$(GREEN)[OK] FUSE is available (macOS)$(RESET)"; \
|
||||
else \
|
||||
echo "$(RED)❌ FUSE not found. Please install:$(RESET)"; \
|
||||
echo " Ubuntu/Debian: sudo apt-get install fuse"; \
|
||||
echo " CentOS/RHEL: sudo yum install fuse"; \
|
||||
echo " macOS: brew install macfuse"; \
|
||||
exit 1; \
|
||||
fi
|
||||
|
||||
check-go:
|
||||
@go version | grep -Eq "go(1\.(2[1-9]|[3-9][0-9]+)|[2-9]\.)" || \
|
||||
(echo "$(RED)❌ Go $(GO_VERSION)+ required. Current: $$(go version)$(RESET)" && exit 1)
|
||||
@echo "$(GREEN)[OK] Go version check passed$(RESET)"
|
||||
|
||||
check-prereqs: check-go check-fuse check-binary
|
||||
@echo "$(GREEN)[OK] All prerequisites satisfied$(RESET)"
|
||||
|
||||
# Setup and initialization
|
||||
|
||||
|
||||
setup-reports:
|
||||
@mkdir -p $(REPORT_DIR)
|
||||
@mkdir -p $(EXTERNAL_TOOLS_DIR)
|
||||
|
||||
setup-external-tools: setup-reports
|
||||
@echo "$(BLUE)🛠️ Setting up external POSIX test tools...$(RESET)"
|
||||
@$(MAKE_SELF) setup-pjdfstest || echo "$(YELLOW)[WARNING] pjdfstest setup failed - continuing without it$(RESET)"
|
||||
@$(MAKE_SELF) setup-nfstest || echo "$(YELLOW)[WARNING] nfstest setup failed - continuing without it$(RESET)"
|
||||
@$(MAKE_SELF) setup-fio || echo "$(YELLOW)[WARNING] FIO setup failed - continuing without it$(RESET)"
|
||||
|
||||
# External tools setup
|
||||
setup-pjdfstest:
|
||||
@if [ ! -d "$(EXTERNAL_TOOLS_DIR)/pjdfstest" ]; then \
|
||||
echo "$(BLUE)[SETUP] Setting up pjdfstest...$(RESET)"; \
|
||||
if ! command -v autoreconf >/dev/null 2>&1; then \
|
||||
echo "$(YELLOW)[WARNING] autoreconf not found - installing build-essential...$(RESET)"; \
|
||||
if command -v apt-get >/dev/null 2>&1; then \
|
||||
sudo apt-get update && sudo apt-get install -y build-essential autoconf automake libtool; \
|
||||
elif command -v yum >/dev/null 2>&1; then \
|
||||
sudo yum install -y gcc make autoconf automake libtool; \
|
||||
else \
|
||||
echo "$(YELLOW)[WARNING] Please install build tools manually$(RESET)"; \
|
||||
exit 1; \
|
||||
fi; \
|
||||
fi; \
|
||||
cd $(EXTERNAL_TOOLS_DIR) && \
|
||||
git clone https://github.com/pjd/pjdfstest.git && \
|
||||
cd pjdfstest && \
|
||||
autoreconf -ifs && \
|
||||
./configure && \
|
||||
make; \
|
||||
else \
|
||||
echo "$(GREEN)[OK] pjdfstest already setup$(RESET)"; \
|
||||
fi
|
||||
|
||||
setup-nfstest:
|
||||
@echo "$(BLUE)[INSTALL] Installing nfstest...$(RESET)"
|
||||
@pip3 install --user nfstest || \
|
||||
echo "$(YELLOW)[WARNING] nfstest installation failed. Install manually: pip3 install nfstest$(RESET)"
|
||||
|
||||
setup-fio:
|
||||
@if ! command -v fio >/dev/null 2>&1; then \
|
||||
echo "$(BLUE)[SETUP] Installing FIO...$(RESET)"; \
|
||||
if command -v apt-get >/dev/null 2>&1; then \
|
||||
sudo apt-get update && sudo apt-get install -y fio; \
|
||||
elif command -v yum >/dev/null 2>&1; then \
|
||||
sudo yum install -y fio; \
|
||||
elif command -v brew >/dev/null 2>&1; then \
|
||||
brew install fio; \
|
||||
else \
|
||||
echo "$(YELLOW)[WARNING] Please install FIO manually$(RESET)"; \
|
||||
fi; \
|
||||
else \
|
||||
echo "$(GREEN)[OK] FIO already installed$(RESET)"; \
|
||||
fi
|
||||
|
||||
# Core test execution
|
||||
test-posix-basic: check-prereqs setup-reports
|
||||
@echo "$(CYAN)[TEST] Running basic POSIX compliance tests...$(RESET)"
|
||||
ifneq ($(TEST_MOUNT_POINT),)
|
||||
@echo "$(BLUE)Using external mount point: $(TEST_MOUNT_POINT)$(RESET)"
|
||||
endif
|
||||
@$(GO_TEST_PREFIX) go test -v -timeout $(TEST_TIMEOUT) -failfast=false -run TestPOSIXCompliance . 2>&1 | \
|
||||
tee $(REPORT_DIR)/posix_basic_results.log
|
||||
|
||||
test-posix-extended: check-prereqs setup-reports
|
||||
@echo "$(CYAN)[TEST] Running extended POSIX compliance tests...$(RESET)"
|
||||
ifneq ($(TEST_MOUNT_POINT),)
|
||||
@echo "$(BLUE)Using external mount point: $(TEST_MOUNT_POINT)$(RESET)"
|
||||
endif
|
||||
@$(GO_TEST_PREFIX) go test -v -timeout $(TEST_TIMEOUT) -failfast=false -run TestPOSIXExtended . 2>&1 | \
|
||||
tee $(REPORT_DIR)/posix_extended_results.log
|
||||
|
||||
test-posix-external: check-prereqs setup-reports
|
||||
@echo "$(CYAN)[TEST] Running external POSIX test suite integration...$(RESET)"
|
||||
ifneq ($(TEST_MOUNT_POINT),)
|
||||
@echo "$(BLUE)Using external mount point: $(TEST_MOUNT_POINT)$(RESET)"
|
||||
endif
|
||||
@if [ -d "$(EXTERNAL_TOOLS_DIR)/pjdfstest" ] || command -v nfstest_posix >/dev/null 2>&1; then \
|
||||
echo "$(GREEN)[OK] External tools available - running external tests$(RESET)"; \
|
||||
$(GO_TEST_PREFIX) go test -v -timeout $(TEST_TIMEOUT) -failfast=false -run TestExternalPOSIXSuites . 2>&1 | \
|
||||
tee $(REPORT_DIR)/posix_external_results.log; \
|
||||
else \
|
||||
echo "$(YELLOW)[WARNING] External tools not available - skipping external tests$(RESET)"; \
|
||||
echo "External tests skipped - tools not available" > $(REPORT_DIR)/posix_external_results.log; \
|
||||
fi
|
||||
|
||||
# Comprehensive test suites
|
||||
test-posix-full: test-posix-basic test-posix-extended test-posix-external
|
||||
@echo "$(GREEN)[OK] Full POSIX compliance test suite completed$(RESET)"
|
||||
@$(MAKE_SELF) generate-report
|
||||
|
||||
test-posix-critical: check-prereqs setup-reports
|
||||
@echo "$(CYAN)[TEST] Running critical POSIX compliance tests...$(RESET)"
|
||||
ifneq ($(TEST_MOUNT_POINT),)
|
||||
@echo "$(BLUE)Using external mount point: $(TEST_MOUNT_POINT)$(RESET)"
|
||||
endif
|
||||
@$(GO_TEST_PREFIX) go test -v -timeout 15m -failfast=false \
|
||||
-run "TestPOSIXCompliance/(FileOperations|DirectoryOperations|PermissionTests|IOOperations)" \
|
||||
. 2>&1 | tee $(REPORT_DIR)/posix_critical_results.log
|
||||
|
||||
test-posix-stress: check-prereqs setup-reports
|
||||
@echo "$(CYAN)[TEST] Running POSIX stress tests...$(RESET)"
|
||||
ifneq ($(TEST_MOUNT_POINT),)
|
||||
@echo "$(BLUE)Using external mount point: $(TEST_MOUNT_POINT)$(RESET)"
|
||||
endif
|
||||
@if [ -d "$(EXTERNAL_TOOLS_DIR)/pjdfstest" ] || command -v nfstest_posix >/dev/null 2>&1; then \
|
||||
echo "$(GREEN)[OK] External tools available - running stress tests$(RESET)"; \
|
||||
$(GO_TEST_PREFIX) go test -v -timeout $(TEST_TIMEOUT) -failfast=false \
|
||||
-run "TestExternalPOSIXSuites/CustomPOSIXTests" \
|
||||
. 2>&1 | tee $(REPORT_DIR)/posix_stress_results.log; \
|
||||
else \
|
||||
echo "$(YELLOW)[WARNING] External tools not available - skipping stress tests$(RESET)"; \
|
||||
echo "Stress tests skipped - tools not available" > $(REPORT_DIR)/posix_stress_results.log; \
|
||||
fi
|
||||
|
||||
# Performance and benchmarks
|
||||
benchmark-posix: check-prereqs setup-reports
|
||||
@echo "$(CYAN)📈 Running POSIX performance benchmarks...$(RESET)"
|
||||
ifneq ($(TEST_MOUNT_POINT),)
|
||||
@echo "$(BLUE)Using external mount point: $(TEST_MOUNT_POINT)$(RESET)"
|
||||
endif
|
||||
@$(GO_TEST_PREFIX) go test -v -timeout $(TEST_TIMEOUT) -run '^$$' -bench=. -benchmem \
|
||||
. 2>&1 | \
|
||||
tee $(REPORT_DIR)/posix_benchmark_results.log
|
||||
|
||||
profile-posix: check-prereqs setup-reports
|
||||
@echo "$(CYAN)[PROFILE] Running POSIX tests with profiling...$(RESET)"
|
||||
ifneq ($(TEST_MOUNT_POINT),)
|
||||
@echo "$(BLUE)Using external mount point: $(TEST_MOUNT_POINT)$(RESET)"
|
||||
endif
|
||||
@$(GO_TEST_PREFIX) go test -v -timeout $(TEST_TIMEOUT) -cpuprofile $(REPORT_DIR)/posix.cpu.prof \
|
||||
-memprofile $(REPORT_DIR)/posix.mem.prof -run TestPOSIXCompliance .
|
||||
@echo "$(GREEN)[PROFILE] Profiles generated:$(RESET)"
|
||||
@echo " CPU: $(REPORT_DIR)/posix.cpu.prof"
|
||||
@echo " Memory: $(REPORT_DIR)/posix.mem.prof"
|
||||
@echo "$(BLUE)View with: go tool pprof $(REPORT_DIR)/posix.cpu.prof$(RESET)"
|
||||
|
||||
# Coverage analysis
|
||||
coverage-posix: check-prereqs setup-reports
|
||||
@echo "$(CYAN)[COVERAGE] Running POSIX tests with coverage analysis...$(RESET)"
|
||||
ifneq ($(TEST_MOUNT_POINT),)
|
||||
@echo "$(BLUE)Using external mount point: $(TEST_MOUNT_POINT)$(RESET)"
|
||||
endif
|
||||
@$(GO_TEST_PREFIX) go test -v -timeout $(TEST_TIMEOUT) -coverprofile=$(REPORT_DIR)/$(COVERAGE_FILE) \
|
||||
.
|
||||
@go tool cover -html=$(REPORT_DIR)/$(COVERAGE_FILE) -o $(REPORT_DIR)/posix_coverage.html
|
||||
@echo "$(GREEN)[COVERAGE] Coverage report generated: $(REPORT_DIR)/posix_coverage.html$(RESET)"
|
||||
|
||||
# The external suites exercise whatever filesystem MOUNT_POINT resolves to;
|
||||
# mkdir -p would happily point them at a plain local directory and report a
|
||||
# pass the mount never earned.
|
||||
require-mounted:
|
||||
@mnt="$(MOUNT_POINT)"; mnt="$${mnt%/}"; \
|
||||
if ! mountpoint -q "$$mnt" 2>/dev/null && ! mount | grep -qF " $$mnt "; then \
|
||||
echo "$(RED)❌ $$mnt is not a mounted filesystem - refusing to run against the local disk$(RESET)"; \
|
||||
exit 1; \
|
||||
fi
|
||||
|
||||
# External tool tests
|
||||
test-pjdfstest: setup-pjdfstest setup-reports require-mounted
|
||||
@echo "$(CYAN)[TEST] Running pjdfstest suite...$(RESET)"
|
||||
@if [ -d "$(EXTERNAL_TOOLS_DIR)/pjdfstest" ]; then \
|
||||
mkdir -p $(MOUNT_POINT)/pjdfstest_workdir; \
|
||||
cd $(MOUNT_POINT)/pjdfstest_workdir && \
|
||||
prove -r $(CURDIR)/$(EXTERNAL_TOOLS_DIR)/pjdfstest/tests/ 2>&1 | tee $(CURDIR)/$(REPORT_DIR)/pjdfstest_results.log; \
|
||||
else \
|
||||
echo "$(RED)❌ pjdfstest not setup$(RESET)"; \
|
||||
exit 1; \
|
||||
fi
|
||||
|
||||
test-nfstest-posix: setup-nfstest setup-reports require-mounted
|
||||
@echo "$(CYAN)[TEST] Running nfstest_posix...$(RESET)"
|
||||
@if command -v nfstest_posix >/dev/null 2>&1; then \
|
||||
mkdir -p $(MOUNT_POINT); \
|
||||
nfstest_posix --path $(MOUNT_POINT) --verbose 2>&1 | \
|
||||
tee $(REPORT_DIR)/nfstest_results.log; \
|
||||
else \
|
||||
echo "$(YELLOW)[WARNING] nfstest_posix not available$(RESET)"; \
|
||||
fi
|
||||
|
||||
# FIO-based performance tests
|
||||
test-fio-posix: setup-reports require-mounted
|
||||
@echo "$(CYAN)[TEST] Running FIO-based POSIX I/O tests...$(RESET)"
|
||||
@$(MAKE_SELF) create-fio-configs
|
||||
@if command -v fio >/dev/null 2>&1; then \
|
||||
mkdir -p $(MOUNT_POINT); \
|
||||
for config in $(REPORT_DIR)/fio_*.conf; do \
|
||||
echo "$(BLUE)Running FIO config: $$config$(RESET)"; \
|
||||
fio $$config --output=$(REPORT_DIR)/$$(basename $$config .conf)_results.log; \
|
||||
done; \
|
||||
else \
|
||||
echo "$(YELLOW)[WARNING] FIO not available$(RESET)"; \
|
||||
fi
|
||||
|
||||
create-fio-configs: setup-reports
|
||||
@echo "$(BLUE)[CONFIG] Creating FIO test configurations...$(RESET)"
|
||||
@echo "[global]" > $(REPORT_DIR)/fio_random_rw.conf
|
||||
@echo "name=posix_random_rw" >> $(REPORT_DIR)/fio_random_rw.conf
|
||||
@echo "ioengine=sync" >> $(REPORT_DIR)/fio_random_rw.conf
|
||||
@echo "iodepth=1" >> $(REPORT_DIR)/fio_random_rw.conf
|
||||
@echo "rw=randrw" >> $(REPORT_DIR)/fio_random_rw.conf
|
||||
@echo "bs=4k" >> $(REPORT_DIR)/fio_random_rw.conf
|
||||
@echo "direct=0" >> $(REPORT_DIR)/fio_random_rw.conf
|
||||
@echo "size=100m" >> $(REPORT_DIR)/fio_random_rw.conf
|
||||
@echo "numjobs=4" >> $(REPORT_DIR)/fio_random_rw.conf
|
||||
@echo "runtime=30" >> $(REPORT_DIR)/fio_random_rw.conf
|
||||
@echo "time_based" >> $(REPORT_DIR)/fio_random_rw.conf
|
||||
@echo "" >> $(REPORT_DIR)/fio_random_rw.conf
|
||||
@echo "[random_rw_test]" >> $(REPORT_DIR)/fio_random_rw.conf
|
||||
@echo "directory=$(MOUNT_POINT)" >> $(REPORT_DIR)/fio_random_rw.conf
|
||||
@echo "[global]" > $(REPORT_DIR)/fio_sequential.conf
|
||||
@echo "name=posix_sequential" >> $(REPORT_DIR)/fio_sequential.conf
|
||||
@echo "ioengine=sync" >> $(REPORT_DIR)/fio_sequential.conf
|
||||
@echo "iodepth=1" >> $(REPORT_DIR)/fio_sequential.conf
|
||||
@echo "bs=1m" >> $(REPORT_DIR)/fio_sequential.conf
|
||||
@echo "direct=0" >> $(REPORT_DIR)/fio_sequential.conf
|
||||
@echo "size=500m" >> $(REPORT_DIR)/fio_sequential.conf
|
||||
@echo "runtime=60" >> $(REPORT_DIR)/fio_sequential.conf
|
||||
@echo "time_based" >> $(REPORT_DIR)/fio_sequential.conf
|
||||
@echo "" >> $(REPORT_DIR)/fio_sequential.conf
|
||||
@echo "[seq_write]" >> $(REPORT_DIR)/fio_sequential.conf
|
||||
@echo "rw=write" >> $(REPORT_DIR)/fio_sequential.conf
|
||||
@echo "directory=$(MOUNT_POINT)" >> $(REPORT_DIR)/fio_sequential.conf
|
||||
@echo "" >> $(REPORT_DIR)/fio_sequential.conf
|
||||
@echo "[seq_read]" >> $(REPORT_DIR)/fio_sequential.conf
|
||||
@echo "rw=read" >> $(REPORT_DIR)/fio_sequential.conf
|
||||
@echo "directory=$(MOUNT_POINT)" >> $(REPORT_DIR)/fio_sequential.conf
|
||||
|
||||
# Reporting and analysis
|
||||
generate-report: setup-reports
|
||||
@echo "$(BLUE)[REPORT] Generating comprehensive POSIX compliance report...$(RESET)"
|
||||
@$(MAKE_SELF) generate-html-report
|
||||
@$(MAKE_SELF) generate-text-report
|
||||
@$(MAKE_SELF) generate-json-report
|
||||
|
||||
generate-html-report:
|
||||
@echo "$(BLUE)[REPORT] Generating HTML report...$(RESET)"
|
||||
@echo "<!DOCTYPE html>" > $(REPORT_DIR)/posix_compliance_report.html
|
||||
@echo "<html><head><title>SeaweedFS POSIX Compliance Report</title>" >> $(REPORT_DIR)/posix_compliance_report.html
|
||||
@echo "<style>body{font-family:Arial;margin:20px}.pass{color:green}.fail{color:red}.summary{background:#f5f5f5;padding:10px;margin:10px 0}.test-section{margin:20px 0;border-left:3px solid #ddd;padding-left:15px}</style>" >> $(REPORT_DIR)/posix_compliance_report.html
|
||||
@echo "</head><body><h1>SeaweedFS POSIX Compliance Report</h1>" >> $(REPORT_DIR)/posix_compliance_report.html
|
||||
@echo "<p>Generated on: $$(date)</p>" >> $(REPORT_DIR)/posix_compliance_report.html
|
||||
@echo "<div class='summary'>" >> $(REPORT_DIR)/posix_compliance_report.html
|
||||
@echo "<h2>Test Summary</h2>" >> $(REPORT_DIR)/posix_compliance_report.html
|
||||
@if [ -f "$(REPORT_DIR)/posix_basic_results.log" ]; then \
|
||||
TOTAL=$$(awk '/RUN|PASS|FAIL/{c++} END{print c+0}' $(REPORT_DIR)/posix_basic_results.log 2>/dev/null || echo 0); \
|
||||
PASSED=$$(awk '/PASS:/{c++} END{print c+0}' $(REPORT_DIR)/posix_basic_results.log 2>/dev/null || echo 0); \
|
||||
FAILED=$$(awk '/FAIL:/{c++} END{print c+0}' $(REPORT_DIR)/posix_basic_results.log 2>/dev/null || echo 0); \
|
||||
echo "<p>Basic Tests: <span class='pass'>$$PASSED passed</span>, <span class='fail'>$$FAILED failed</span></p>" >> $(REPORT_DIR)/posix_compliance_report.html; \
|
||||
fi
|
||||
@if [ -f "$(REPORT_DIR)/posix_critical_results.log" ]; then \
|
||||
TOTAL=$$(awk '/RUN|PASS|FAIL/{c++} END{print c+0}' $(REPORT_DIR)/posix_critical_results.log 2>/dev/null || echo 0); \
|
||||
PASSED=$$(awk '/PASS:/{c++} END{print c+0}' $(REPORT_DIR)/posix_critical_results.log 2>/dev/null || echo 0); \
|
||||
FAILED=$$(awk '/FAIL:/{c++} END{print c+0}' $(REPORT_DIR)/posix_critical_results.log 2>/dev/null || echo 0); \
|
||||
echo "<p>Critical Tests: <span class='pass'>$$PASSED passed</span>, <span class='fail'>$$FAILED failed</span></p>" >> $(REPORT_DIR)/posix_compliance_report.html; \
|
||||
fi
|
||||
@echo "</div>" >> $(REPORT_DIR)/posix_compliance_report.html
|
||||
@echo "<h2>Detailed Results</h2>" >> $(REPORT_DIR)/posix_compliance_report.html
|
||||
@for logfile in $(REPORT_DIR)/*.log; do \
|
||||
if [ -f "$$logfile" ]; then \
|
||||
basename=$$(basename "$$logfile" .log); \
|
||||
echo "<div class='test-section'>" >> $(REPORT_DIR)/posix_compliance_report.html; \
|
||||
echo "<h3>$$basename</h3>" >> $(REPORT_DIR)/posix_compliance_report.html; \
|
||||
echo "<pre>" >> $(REPORT_DIR)/posix_compliance_report.html; \
|
||||
tail -50 "$$logfile" | sed 's/&/\&/g; s/</\</g; s/>/\>/g' >> $(REPORT_DIR)/posix_compliance_report.html; \
|
||||
echo "</pre></div>" >> $(REPORT_DIR)/posix_compliance_report.html; \
|
||||
fi; \
|
||||
done
|
||||
@echo "</body></html>" >> $(REPORT_DIR)/posix_compliance_report.html
|
||||
@echo "$(GREEN)[OK] HTML report generated: $(REPORT_DIR)/posix_compliance_report.html$(RESET)"
|
||||
|
||||
generate-text-report:
|
||||
@echo "$(BLUE)[REPORT] Generating text report...$(RESET)"
|
||||
@echo "SeaweedFS POSIX Compliance Report" > $(REPORT_DIR)/posix_compliance_summary.txt
|
||||
@echo "=================================" >> $(REPORT_DIR)/posix_compliance_summary.txt
|
||||
@echo "" >> $(REPORT_DIR)/posix_compliance_summary.txt
|
||||
@echo "Generated: $$(date)" >> $(REPORT_DIR)/posix_compliance_summary.txt
|
||||
@echo "SeaweedFS Version: $$($(WEED_BINARY) version 2>/dev/null || echo 'Unknown')" >> $(REPORT_DIR)/posix_compliance_summary.txt
|
||||
@echo "" >> $(REPORT_DIR)/posix_compliance_summary.txt
|
||||
@echo "Test Summary: See individual log files for detailed results" >> $(REPORT_DIR)/posix_compliance_summary.txt
|
||||
@echo "$(GREEN)[OK] Text report generated: $(REPORT_DIR)/posix_compliance_summary.txt$(RESET)"
|
||||
|
||||
generate-json-report:
|
||||
@echo "$(BLUE)[REPORT] Generating JSON report...$(RESET)"
|
||||
@echo "{" > $(REPORT_DIR)/posix_compliance_report.json
|
||||
@echo " \"report_type\": \"posix_compliance\"," >> $(REPORT_DIR)/posix_compliance_report.json
|
||||
@echo " \"timestamp\": \"$$(date -u +"%Y-%m-%dT%H:%M:%SZ")\"," >> $(REPORT_DIR)/posix_compliance_report.json
|
||||
@echo " \"seaweedfs_version\": \"$$($(WEED_BINARY) version 2>/dev/null | head -n1 | sed 's/\\/\\\\/g; s/\"/\\\"/g' || echo 'Unknown')\"," >> $(REPORT_DIR)/posix_compliance_report.json
|
||||
@echo " \"test_environment\": { \"os\": \"$$(uname -s)\", \"arch\": \"$$(uname -m)\" }," >> $(REPORT_DIR)/posix_compliance_report.json
|
||||
@echo " \"test_suites\": {" >> $(REPORT_DIR)/posix_compliance_report.json
|
||||
@FIRST=true; \
|
||||
for logfile in $(REPORT_DIR)/*.log; do \
|
||||
if [ -f "$$logfile" ]; then \
|
||||
basename=$$(basename "$$logfile" .log); \
|
||||
if [ "$$FIRST" = "true" ]; then \
|
||||
FIRST=false; \
|
||||
else \
|
||||
echo " ," >> $(REPORT_DIR)/posix_compliance_report.json; \
|
||||
fi; \
|
||||
PASSED=$$(awk '/PASS:/{c++} END{print c+0}' "$$logfile" 2>/dev/null || echo 0); \
|
||||
FAILED=$$(awk '/FAIL:/{c++} END{print c+0}' "$$logfile" 2>/dev/null || echo 0); \
|
||||
TOTAL=$$(expr "$$PASSED" + "$$FAILED" 2>/dev/null || echo 0); \
|
||||
echo " \"$$basename\": {" >> $(REPORT_DIR)/posix_compliance_report.json; \
|
||||
echo " \"total_tests\": $$TOTAL," >> $(REPORT_DIR)/posix_compliance_report.json; \
|
||||
echo " \"passed\": $$PASSED," >> $(REPORT_DIR)/posix_compliance_report.json; \
|
||||
echo " \"failed\": $$FAILED," >> $(REPORT_DIR)/posix_compliance_report.json; \
|
||||
if [ "$$TOTAL" -gt 0 ] 2>/dev/null; then \
|
||||
SUCCESS_RATE=$$(awk "BEGIN {printf \"%.2f\", ($$PASSED/$$TOTAL)*100}" 2>/dev/null || echo "0.00"); \
|
||||
else \
|
||||
SUCCESS_RATE="0.00"; \
|
||||
fi; \
|
||||
echo " \"success_rate\": \"$$SUCCESS_RATE%\"" >> $(REPORT_DIR)/posix_compliance_report.json; \
|
||||
echo " }" >> $(REPORT_DIR)/posix_compliance_report.json; \
|
||||
fi; \
|
||||
done
|
||||
@echo " }," >> $(REPORT_DIR)/posix_compliance_report.json
|
||||
@TOTAL_PASSED=0; TOTAL_FAILED=0; \
|
||||
for logfile in $(REPORT_DIR)/*.log; do \
|
||||
if [ -f "$$logfile" ]; then \
|
||||
PASSED=$$(awk '/PASS:/{c++} END{print c+0}' "$$logfile" 2>/dev/null || echo 0); \
|
||||
FAILED=$$(awk '/FAIL:/{c++} END{print c+0}' "$$logfile" 2>/dev/null || echo 0); \
|
||||
TOTAL_PASSED=$$(expr "$$TOTAL_PASSED" + "$$PASSED" 2>/dev/null || echo "$$TOTAL_PASSED"); \
|
||||
TOTAL_FAILED=$$(expr "$$TOTAL_FAILED" + "$$FAILED" 2>/dev/null || echo "$$TOTAL_FAILED"); \
|
||||
fi; \
|
||||
done; \
|
||||
GRAND_TOTAL=$$(expr "$$TOTAL_PASSED" + "$$TOTAL_FAILED" 2>/dev/null || echo 0); \
|
||||
if [ "$$GRAND_TOTAL" -gt 0 ] 2>/dev/null; then \
|
||||
OVERALL_SUCCESS=$$(awk "BEGIN {printf \"%.2f\", ($$TOTAL_PASSED/$$GRAND_TOTAL)*100}" 2>/dev/null || echo "0.00"); \
|
||||
else \
|
||||
OVERALL_SUCCESS="0.00"; \
|
||||
fi; \
|
||||
echo " \"summary\": {" >> $(REPORT_DIR)/posix_compliance_report.json; \
|
||||
echo " \"total_tests\": $$GRAND_TOTAL," >> $(REPORT_DIR)/posix_compliance_report.json; \
|
||||
echo " \"total_passed\": $$TOTAL_PASSED," >> $(REPORT_DIR)/posix_compliance_report.json; \
|
||||
echo " \"total_failed\": $$TOTAL_FAILED," >> $(REPORT_DIR)/posix_compliance_report.json; \
|
||||
echo " \"overall_success_rate\": \"$$OVERALL_SUCCESS%\"" >> $(REPORT_DIR)/posix_compliance_report.json; \
|
||||
echo " }" >> $(REPORT_DIR)/posix_compliance_report.json
|
||||
@echo "}" >> $(REPORT_DIR)/posix_compliance_report.json
|
||||
@echo "$(GREEN)[OK] JSON report generated: $(REPORT_DIR)/posix_compliance_report.json$(RESET)"
|
||||
|
||||
# Cleanup and maintenance
|
||||
clean:
|
||||
@echo "$(YELLOW)🧹 Cleaning up test artifacts...$(RESET)"
|
||||
@rm -rf $(REPORT_DIR)
|
||||
@rm -rf /tmp/seaweedfs_*_test_*
|
||||
@go clean -testcache
|
||||
@echo "$(GREEN)[OK] Cleanup complete$(RESET)"
|
||||
|
||||
clean-external-tools:
|
||||
@echo "$(YELLOW)🧹 Cleaning up external tools...$(RESET)"
|
||||
@rm -rf $(EXTERNAL_TOOLS_DIR)
|
||||
|
||||
clean-all: clean clean-external-tools
|
||||
|
||||
# Development and debugging
|
||||
validate:
|
||||
@echo "$(BLUE)[OK] Validating test files...$(RESET)"
|
||||
@go build -o /dev/null ./...
|
||||
@echo "$(GREEN)[OK] All test files compile successfully$(RESET)"
|
||||
|
||||
fmt:
|
||||
@echo "$(BLUE)🎨 Formatting Go code...$(RESET)"
|
||||
@go fmt ./...
|
||||
|
||||
lint:
|
||||
@echo "$(BLUE)🔍 Running linter...$(RESET)"
|
||||
@if command -v golangci-lint >/dev/null 2>&1; then \
|
||||
golangci-lint run; \
|
||||
else \
|
||||
echo "$(YELLOW)[WARNING] golangci-lint not found, running go vet instead$(RESET)"; \
|
||||
go vet ./...; \
|
||||
fi
|
||||
|
||||
# CI/CD integration
|
||||
ci-posix-tests: check-prereqs setup-external-tools
|
||||
@echo "$(CYAN)🚀 Running POSIX tests for CI/CD...$(RESET)"
|
||||
@$(MAKE_SELF) test-posix-critical
|
||||
@$(MAKE_SELF) generate-report
|
||||
|
||||
|
||||
|
||||
# Documentation and help
|
||||
list-tests:
|
||||
@echo "$(BLUE)📋 Available POSIX test functions:$(RESET)"
|
||||
@grep -r "^func Test" *.go 2>/dev/null | sed 's/.*func \(Test[^(]*\).*/ \1/' | sort || echo "No test files found"
|
||||
|
||||
test-info:
|
||||
@echo "$(BLUE)[INFO] POSIX Test Information:$(RESET)"
|
||||
@echo "Test files:"
|
||||
@echo " - $(POSIX_BASIC_TESTS): Core POSIX compliance tests"
|
||||
@echo " - $(POSIX_EXTENDED_TESTS): Extended POSIX feature tests"
|
||||
@echo " - $(POSIX_EXTERNAL_TESTS): External test suite integration"
|
||||
@echo ""
|
||||
@echo "External tools:"
|
||||
@echo " - pjdfstest: Comprehensive POSIX filesystem test suite"
|
||||
@echo " - nfstest: Network filesystem POSIX API verification"
|
||||
@echo " - FIO: Flexible I/O performance testing"
|
||||
|
||||
help:
|
||||
@echo "$(CYAN)SeaweedFS POSIX Compliance Testing$(RESET)"
|
||||
@echo "=================================="
|
||||
@echo ""
|
||||
@echo "$(WHITE)Prerequisites:$(RESET)"
|
||||
@echo " $(GREEN)make check-prereqs$(RESET) - Check all prerequisites"
|
||||
@echo " $(GREEN)make setup-external-tools$(RESET) - Setup external POSIX test tools"
|
||||
@echo ""
|
||||
@echo "$(WHITE)Core POSIX Tests:$(RESET)"
|
||||
@echo " $(GREEN)make test-posix-basic$(RESET) - Run basic POSIX compliance tests"
|
||||
@echo " $(GREEN)make test-posix-extended$(RESET) - Run extended POSIX feature tests"
|
||||
@echo " $(GREEN)make test-posix-external$(RESET) - Run external test suite integration"
|
||||
@echo " $(GREEN)make test-posix-full$(RESET) - Run complete POSIX test suite"
|
||||
@echo ""
|
||||
@echo "$(WHITE)Focused Tests:$(RESET)"
|
||||
@echo " $(GREEN)make test-posix-critical$(RESET) - Run critical POSIX compliance tests"
|
||||
@echo " $(GREEN)make test-posix-stress$(RESET) - Run POSIX stress tests"
|
||||
@echo ""
|
||||
@echo "$(WHITE)Performance & Analysis:$(RESET)"
|
||||
@echo " $(GREEN)make benchmark-posix$(RESET) - Run POSIX performance benchmarks"
|
||||
@echo " $(GREEN)make profile-posix$(RESET) - Run tests with performance profiling"
|
||||
@echo " $(GREEN)make coverage-posix$(RESET) - Run tests with coverage analysis"
|
||||
@echo " $(GREEN)make test-fio-posix$(RESET) - Run FIO-based I/O performance tests"
|
||||
@echo ""
|
||||
@echo "$(WHITE)External Test Suites:$(RESET)"
|
||||
@echo " $(GREEN)make test-pjdfstest$(RESET) - Run pjdfstest suite"
|
||||
@echo " $(GREEN)make test-nfstest-posix$(RESET) - Run nfstest_posix"
|
||||
@echo ""
|
||||
@echo "$(WHITE)Reporting:$(RESET)"
|
||||
@echo " $(GREEN)make generate-report$(RESET) - Generate comprehensive compliance report"
|
||||
@echo ""
|
||||
@echo "$(WHITE)Development:$(RESET)"
|
||||
@echo " $(GREEN)make validate$(RESET) - Validate test file compilation"
|
||||
@echo " $(GREEN)make fmt$(RESET) - Format Go code"
|
||||
@echo " $(GREEN)make lint$(RESET) - Run linter"
|
||||
@echo " $(GREEN)make list-tests$(RESET) - List available test functions"
|
||||
@echo ""
|
||||
@echo "$(WHITE)Maintenance:$(RESET)"
|
||||
@echo " $(GREEN)make clean$(RESET) - Clean up test artifacts"
|
||||
@echo " $(GREEN)make clean-all$(RESET) - Clean everything including external tools"
|
||||
@echo ""
|
||||
@echo "$(WHITE)CI/CD Integration:$(RESET)"
|
||||
@echo " $(GREEN)make ci-posix-tests$(RESET) - Run POSIX tests optimized for CI/CD"
|
||||
|
||||
|
||||
.PHONY: help check-prereqs check-binary check-fuse check-go setup-reports \
|
||||
setup-external-tools setup-pjdfstest setup-nfstest setup-fio \
|
||||
test-posix-basic test-posix-extended test-posix-external test-posix-full \
|
||||
test-posix-critical test-posix-stress benchmark-posix profile-posix coverage-posix \
|
||||
test-pjdfstest test-nfstest-posix test-fio-posix create-fio-configs \
|
||||
generate-report generate-html-report generate-text-report generate-json-report \
|
||||
clean clean-external-tools clean-all validate fmt lint ci-posix-tests \
|
||||
list-tests test-info
|
||||
@@ -0,0 +1,713 @@
|
||||
//go:build !windows
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// POSIXComplianceTestSuite provides comprehensive POSIX compliance testing for FUSE mounts
|
||||
type POSIXComplianceTestSuite struct {
|
||||
framework *FuseTestFramework
|
||||
t *testing.T
|
||||
}
|
||||
|
||||
// NewPOSIXComplianceTestSuite creates a new POSIX compliance test suite
|
||||
func NewPOSIXComplianceTestSuite(t *testing.T, framework *FuseTestFramework) *POSIXComplianceTestSuite {
|
||||
return &POSIXComplianceTestSuite{
|
||||
framework: framework,
|
||||
t: t,
|
||||
}
|
||||
}
|
||||
|
||||
// TestPOSIXCompliance runs all POSIX compliance tests
|
||||
func TestPOSIXCompliance(t *testing.T) {
|
||||
config := DefaultTestConfig()
|
||||
config.EnableDebug = true
|
||||
config.MountOptions = []string{"-allowOthers", "-nonempty"}
|
||||
|
||||
framework := NewFuseTestFramework(t, config)
|
||||
defer framework.Cleanup()
|
||||
require.NoError(t, framework.Setup(config))
|
||||
|
||||
suite := NewPOSIXComplianceTestSuite(t, framework)
|
||||
|
||||
// Run all POSIX compliance test categories
|
||||
t.Run("FileOperations", suite.TestFileOperations)
|
||||
t.Run("DirectoryOperations", suite.TestDirectoryOperations)
|
||||
t.Run("SymlinkOperations", suite.TestSymlinkOperations)
|
||||
t.Run("PermissionTests", suite.TestPermissions)
|
||||
t.Run("TimestampTests", suite.TestTimestamps)
|
||||
t.Run("IOOperations", suite.TestIOOperations)
|
||||
t.Run("FileDescriptorTests", suite.TestFileDescriptors)
|
||||
t.Run("AtomicOperations", suite.TestAtomicOperations)
|
||||
t.Run("ConcurrentAccess", suite.TestConcurrentAccess)
|
||||
t.Run("ErrorHandling", suite.TestErrorHandling)
|
||||
}
|
||||
|
||||
// TestFileOperations tests POSIX file operation compliance
|
||||
func (s *POSIXComplianceTestSuite) TestFileOperations(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("CreateFile", func(t *testing.T) {
|
||||
filepath := filepath.Join(mountPoint, "test_create.txt")
|
||||
|
||||
// Test file creation with O_CREAT
|
||||
fd, err := syscall.Open(filepath, syscall.O_CREAT|syscall.O_WRONLY, 0644)
|
||||
require.NoError(t, err)
|
||||
require.Greater(t, fd, 0)
|
||||
|
||||
err = syscall.Close(fd)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify file exists
|
||||
_, err = os.Stat(filepath)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("CreateExclusiveFile", func(t *testing.T) {
|
||||
filepath := filepath.Join(mountPoint, "test_excl.txt")
|
||||
|
||||
// First creation should succeed
|
||||
fd, err := syscall.Open(filepath, syscall.O_CREAT|syscall.O_EXCL|syscall.O_WRONLY, 0644)
|
||||
require.NoError(t, err)
|
||||
syscall.Close(fd)
|
||||
|
||||
// Second creation should fail with EEXIST
|
||||
_, err = syscall.Open(filepath, syscall.O_CREAT|syscall.O_EXCL|syscall.O_WRONLY, 0644)
|
||||
require.Error(t, err)
|
||||
require.Equal(t, syscall.EEXIST, err)
|
||||
})
|
||||
|
||||
t.Run("TruncateFile", func(t *testing.T) {
|
||||
filepath := filepath.Join(mountPoint, "test_truncate.txt")
|
||||
content := []byte("Hello, World! This is a test file for truncation.")
|
||||
|
||||
// Create file with content
|
||||
err := os.WriteFile(filepath, content, 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Truncate to 5 bytes
|
||||
err = syscall.Truncate(filepath, 5)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify truncation
|
||||
readContent, err := os.ReadFile(filepath)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []byte("Hello"), readContent)
|
||||
})
|
||||
|
||||
t.Run("UnlinkFile", func(t *testing.T) {
|
||||
filepath := filepath.Join(mountPoint, "test_unlink.txt")
|
||||
|
||||
// Create file
|
||||
err := os.WriteFile(filepath, []byte("test"), 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Unlink file
|
||||
err = syscall.Unlink(filepath)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify file no longer exists
|
||||
_, err = os.Stat(filepath)
|
||||
require.True(t, os.IsNotExist(err))
|
||||
})
|
||||
}
|
||||
|
||||
// TestDirectoryOperations tests POSIX directory operation compliance
|
||||
func (s *POSIXComplianceTestSuite) TestDirectoryOperations(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("CreateDirectory", func(t *testing.T) {
|
||||
dirPath := filepath.Join(mountPoint, "test_mkdir")
|
||||
|
||||
err := syscall.Mkdir(dirPath, 0755)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify directory exists and has correct type
|
||||
stat, err := os.Stat(dirPath)
|
||||
require.NoError(t, err)
|
||||
require.True(t, stat.IsDir())
|
||||
})
|
||||
|
||||
t.Run("RemoveDirectory", func(t *testing.T) {
|
||||
dirPath := filepath.Join(mountPoint, "test_rmdir")
|
||||
|
||||
// Create directory
|
||||
err := os.Mkdir(dirPath, 0755)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Remove directory
|
||||
err = syscall.Rmdir(dirPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify directory no longer exists
|
||||
_, err = os.Stat(dirPath)
|
||||
require.True(t, os.IsNotExist(err))
|
||||
})
|
||||
|
||||
t.Run("RemoveNonEmptyDirectory", func(t *testing.T) {
|
||||
dirPath := filepath.Join(mountPoint, "test_rmdir_nonempty")
|
||||
filePath := filepath.Join(dirPath, "file.txt")
|
||||
|
||||
// Create directory and file
|
||||
err := os.Mkdir(dirPath, 0755)
|
||||
require.NoError(t, err)
|
||||
err = os.WriteFile(filePath, []byte("test"), 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Attempt to remove non-empty directory should fail
|
||||
err = syscall.Rmdir(dirPath)
|
||||
require.Error(t, err)
|
||||
require.Equal(t, syscall.ENOTEMPTY, err)
|
||||
})
|
||||
|
||||
t.Run("RenameDirectory", func(t *testing.T) {
|
||||
oldPath := filepath.Join(mountPoint, "old_dir")
|
||||
newPath := filepath.Join(mountPoint, "new_dir")
|
||||
|
||||
// Create directory
|
||||
err := os.Mkdir(oldPath, 0755)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Rename directory
|
||||
err = os.Rename(oldPath, newPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify old path doesn't exist and new path does
|
||||
_, err = os.Stat(oldPath)
|
||||
require.True(t, os.IsNotExist(err))
|
||||
stat, err := os.Stat(newPath)
|
||||
require.NoError(t, err)
|
||||
require.True(t, stat.IsDir())
|
||||
})
|
||||
}
|
||||
|
||||
// TestSymlinkOperations tests POSIX symlink operation compliance
|
||||
func (s *POSIXComplianceTestSuite) TestSymlinkOperations(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("CreateSymlink", func(t *testing.T) {
|
||||
targetFile := filepath.Join(mountPoint, "target.txt")
|
||||
linkFile := filepath.Join(mountPoint, "link.txt")
|
||||
|
||||
// Create target file
|
||||
err := os.WriteFile(targetFile, []byte("target content"), 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create symlink
|
||||
err = os.Symlink(targetFile, linkFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify symlink properties
|
||||
linkStat, err := os.Lstat(linkFile)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, os.ModeSymlink, linkStat.Mode()&os.ModeType)
|
||||
|
||||
// Verify symlink content
|
||||
linkTarget, err := os.Readlink(linkFile)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, targetFile, linkTarget)
|
||||
|
||||
// Verify following symlink works
|
||||
content, err := os.ReadFile(linkFile)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []byte("target content"), content)
|
||||
})
|
||||
|
||||
t.Run("BrokenSymlink", func(t *testing.T) {
|
||||
nonexistentTarget := filepath.Join(mountPoint, "nonexistent.txt")
|
||||
linkFile := filepath.Join(mountPoint, "broken_link.txt")
|
||||
|
||||
// Create symlink to nonexistent file
|
||||
err := os.Symlink(nonexistentTarget, linkFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Lstat should work (doesn't follow symlink)
|
||||
_, err = os.Lstat(linkFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Stat should fail (follows symlink to nonexistent target)
|
||||
_, err = os.Stat(linkFile)
|
||||
require.Error(t, err)
|
||||
require.True(t, os.IsNotExist(err))
|
||||
})
|
||||
}
|
||||
|
||||
// TestPermissions tests POSIX permission compliance
|
||||
func (s *POSIXComplianceTestSuite) TestPermissions(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("FilePermissions", func(t *testing.T) {
|
||||
testFile := filepath.Join(mountPoint, "perm_test.txt")
|
||||
|
||||
// Create file with specific permissions
|
||||
fd, err := syscall.Open(testFile, syscall.O_CREAT|syscall.O_WRONLY, 0642)
|
||||
require.NoError(t, err)
|
||||
syscall.Close(fd)
|
||||
|
||||
// Verify permissions
|
||||
stat, err := os.Stat(testFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Note: The final file permissions are affected by the system's umask.
|
||||
// We check against the requested mode and the mode as affected by a common umask (e.g. 0022).
|
||||
actualMode := stat.Mode() & os.ModePerm
|
||||
expectedMode := os.FileMode(0642)
|
||||
expectedModeWithUmask := os.FileMode(0640) // e.g., 0642 with umask 0002 or 0022
|
||||
|
||||
// Accept either the exact permissions or permissions as modified by umask
|
||||
if actualMode != expectedMode && actualMode != expectedModeWithUmask {
|
||||
t.Errorf("Expected file permissions %o or %o, but got %o",
|
||||
expectedMode, expectedModeWithUmask, actualMode)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ChangeFilePermissions", func(t *testing.T) {
|
||||
testFile := filepath.Join(mountPoint, "chmod_test.txt")
|
||||
|
||||
// Create file
|
||||
err := os.WriteFile(testFile, []byte("test"), 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Change permissions
|
||||
err = os.Chmod(testFile, 0755)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify new permissions
|
||||
stat, err := os.Stat(testFile)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, os.FileMode(0755), stat.Mode()&os.ModePerm)
|
||||
})
|
||||
|
||||
t.Run("DirectoryPermissions", func(t *testing.T) {
|
||||
testDir := filepath.Join(mountPoint, "perm_dir")
|
||||
|
||||
// Create directory with specific permissions
|
||||
err := syscall.Mkdir(testDir, 0750)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify permissions
|
||||
stat, err := os.Stat(testDir)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, os.FileMode(0750)|os.ModeDir, stat.Mode())
|
||||
})
|
||||
}
|
||||
|
||||
// TestTimestamps tests POSIX timestamp compliance
|
||||
func (s *POSIXComplianceTestSuite) TestTimestamps(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("AccessTime", func(t *testing.T) {
|
||||
testFile := filepath.Join(mountPoint, "atime_test.txt")
|
||||
|
||||
// Create file
|
||||
err := os.WriteFile(testFile, []byte("test content"), 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Get initial timestamps
|
||||
stat1, err := os.Stat(testFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Wait a bit to ensure time difference
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// Read file (should update access time)
|
||||
_, err = os.ReadFile(testFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Get new timestamps
|
||||
stat2, err := os.Stat(testFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Access time should have been updated, and modification time should be unchanged.
|
||||
stat1Sys := stat1.Sys().(*syscall.Stat_t)
|
||||
stat2Sys := stat2.Sys().(*syscall.Stat_t)
|
||||
require.True(t, getAtimeNano(stat2Sys) >= getAtimeNano(stat1Sys), "access time should be updated or stay the same")
|
||||
require.Equal(t, stat1.ModTime(), stat2.ModTime(), "modification time should not change on read")
|
||||
})
|
||||
|
||||
t.Run("ModificationTime", func(t *testing.T) {
|
||||
testFile := filepath.Join(mountPoint, "mtime_test.txt")
|
||||
|
||||
// Create file
|
||||
err := os.WriteFile(testFile, []byte("initial content"), 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Get initial timestamp
|
||||
stat1, err := os.Stat(testFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Wait to ensure time difference
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// Modify file
|
||||
err = os.WriteFile(testFile, []byte("modified content"), 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Get new timestamp
|
||||
stat2, err := os.Stat(testFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Modification time should have changed
|
||||
require.True(t, stat2.ModTime().After(stat1.ModTime()))
|
||||
})
|
||||
|
||||
t.Run("SetTimestamps", func(t *testing.T) {
|
||||
testFile := filepath.Join(mountPoint, "utime_test.txt")
|
||||
|
||||
// Create file
|
||||
err := os.WriteFile(testFile, []byte("test"), 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Set specific timestamps
|
||||
atime := time.Now().Add(-24 * time.Hour)
|
||||
mtime := time.Now().Add(-12 * time.Hour)
|
||||
|
||||
err = os.Chtimes(testFile, atime, mtime)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify timestamps were set
|
||||
stat, err := os.Stat(testFile)
|
||||
require.NoError(t, err)
|
||||
require.True(t, stat.ModTime().Equal(mtime))
|
||||
})
|
||||
}
|
||||
|
||||
// TestIOOperations tests POSIX I/O operation compliance
|
||||
func (s *POSIXComplianceTestSuite) TestIOOperations(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("ReadWrite", func(t *testing.T) {
|
||||
testFile := filepath.Join(mountPoint, "rw_test.txt")
|
||||
testData := []byte("Hello, POSIX World!")
|
||||
|
||||
// Write data
|
||||
fd, err := syscall.Open(testFile, syscall.O_CREAT|syscall.O_WRONLY, 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
n, err := syscall.Write(fd, testData)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(testData), n)
|
||||
|
||||
err = syscall.Close(fd)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Read data back
|
||||
fd, err = syscall.Open(testFile, syscall.O_RDONLY, 0)
|
||||
require.NoError(t, err)
|
||||
|
||||
readBuffer := make([]byte, len(testData))
|
||||
n, err = syscall.Read(fd, readBuffer)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(testData), n)
|
||||
require.Equal(t, testData, readBuffer)
|
||||
|
||||
err = syscall.Close(fd)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Seek", func(t *testing.T) {
|
||||
testFile := filepath.Join(mountPoint, "seek_test.txt")
|
||||
testData := []byte("0123456789ABCDEF")
|
||||
|
||||
// Create file with test data
|
||||
err := os.WriteFile(testFile, testData, 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Open for reading
|
||||
fd, err := syscall.Open(testFile, syscall.O_RDONLY, 0)
|
||||
require.NoError(t, err)
|
||||
defer syscall.Close(fd)
|
||||
|
||||
// Seek to position 5
|
||||
pos, err := syscall.Seek(fd, 5, 0) // SEEK_SET
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(5), pos)
|
||||
|
||||
// Read 3 bytes
|
||||
buffer := make([]byte, 3)
|
||||
n, err := syscall.Read(fd, buffer)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 3, n)
|
||||
require.Equal(t, []byte("567"), buffer)
|
||||
|
||||
// Seek from current position
|
||||
pos, err = syscall.Seek(fd, 2, 1) // SEEK_CUR
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(10), pos)
|
||||
|
||||
// Read 1 byte
|
||||
buffer = make([]byte, 1)
|
||||
n, err = syscall.Read(fd, buffer)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, n)
|
||||
require.Equal(t, []byte("A"), buffer)
|
||||
|
||||
// Test positioned I/O operations (pread/pwrite)
|
||||
syscall.Close(fd)
|
||||
|
||||
// Open for read/write to test pwrite
|
||||
fd, err = syscall.Open(testFile, syscall.O_RDWR, 0)
|
||||
require.NoError(t, err)
|
||||
defer syscall.Close(fd)
|
||||
|
||||
// Positioned write test
|
||||
writeData := []byte("XYZ")
|
||||
n, err = syscall.Pwrite(fd, writeData, 5) // pwrite at offset 5
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(writeData), n)
|
||||
|
||||
// Verify file position is unchanged by pwrite
|
||||
currentPos, err := syscall.Seek(fd, 0, 1) // SEEK_CUR
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(0), currentPos, "file offset should not be changed by pwrite")
|
||||
|
||||
// Read back with pread
|
||||
readBuffer := make([]byte, len(writeData))
|
||||
n, err = syscall.Pread(fd, readBuffer, 5) // pread at offset 5
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(writeData), n)
|
||||
require.Equal(t, writeData, readBuffer)
|
||||
|
||||
// Verify file position is still unchanged by pread
|
||||
currentPos, err = syscall.Seek(fd, 0, 1) // SEEK_CUR
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(0), currentPos, "file offset should not be changed by pread")
|
||||
})
|
||||
|
||||
t.Run("AppendMode", func(t *testing.T) {
|
||||
testFile := filepath.Join(mountPoint, "append_test.txt")
|
||||
|
||||
// Create file with initial content
|
||||
err := os.WriteFile(testFile, []byte("initial"), 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Open in append mode
|
||||
fd, err := syscall.Open(testFile, syscall.O_WRONLY|syscall.O_APPEND, 0)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Write additional content
|
||||
appendData := []byte(" appended")
|
||||
n, err := syscall.Write(fd, appendData)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(appendData), n)
|
||||
|
||||
err = syscall.Close(fd)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify content
|
||||
content, err := os.ReadFile(testFile)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []byte("initial appended"), content)
|
||||
})
|
||||
}
|
||||
|
||||
// TestFileDescriptors tests POSIX file descriptor behavior
|
||||
func (s *POSIXComplianceTestSuite) TestFileDescriptors(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("DuplicateFileDescriptors", func(t *testing.T) {
|
||||
testFile := filepath.Join(mountPoint, "dup_test.txt")
|
||||
testData := []byte("duplicate test")
|
||||
|
||||
// Create and open file
|
||||
fd, err := syscall.Open(testFile, syscall.O_CREAT|syscall.O_RDWR, 0644)
|
||||
require.NoError(t, err)
|
||||
defer syscall.Close(fd)
|
||||
|
||||
// Write initial data
|
||||
n, err := syscall.Write(fd, testData)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(testData), n)
|
||||
|
||||
// Duplicate file descriptor
|
||||
dupFd, err := syscall.Dup(fd)
|
||||
require.NoError(t, err)
|
||||
defer syscall.Close(dupFd)
|
||||
|
||||
// Both descriptors should refer to the same file
|
||||
// Seek on one should affect the other
|
||||
pos, err := syscall.Seek(fd, 0, 0) // SEEK_SET
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(0), pos)
|
||||
|
||||
// Read from duplicate descriptor should start from position 0
|
||||
buffer := make([]byte, 9)
|
||||
n, err = syscall.Read(dupFd, buffer)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 9, n)
|
||||
require.Equal(t, []byte("duplicate"), buffer)
|
||||
})
|
||||
|
||||
t.Run("FileDescriptorFlags", func(t *testing.T) {
|
||||
testFile := filepath.Join(mountPoint, "flags_test.txt")
|
||||
|
||||
// Create file
|
||||
err := os.WriteFile(testFile, []byte("test"), 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Open with close-on-exec flag
|
||||
fd, err := syscall.Open(testFile, syscall.O_RDONLY|syscall.O_CLOEXEC, 0)
|
||||
require.NoError(t, err)
|
||||
defer syscall.Close(fd)
|
||||
|
||||
// Verify close-on-exec flag is set
|
||||
// Note: FcntlInt is not available on all platforms, this test may need platform-specific implementation
|
||||
t.Skip("FcntlInt not available on this platform")
|
||||
})
|
||||
}
|
||||
|
||||
// TestAtomicOperations tests POSIX atomic operation compliance
|
||||
func (s *POSIXComplianceTestSuite) TestAtomicOperations(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("AtomicRename", func(t *testing.T) {
|
||||
oldFile := filepath.Join(mountPoint, "atomic_old.txt")
|
||||
newFile := filepath.Join(mountPoint, "atomic_new.txt")
|
||||
testData := []byte("atomic test data")
|
||||
|
||||
// Create source file
|
||||
err := os.WriteFile(oldFile, testData, 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create existing target file
|
||||
err = os.WriteFile(newFile, []byte("old content"), 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Atomic rename should replace target
|
||||
err = os.Rename(oldFile, newFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify source no longer exists
|
||||
_, err = os.Stat(oldFile)
|
||||
require.True(t, os.IsNotExist(err))
|
||||
|
||||
// Verify target has new content
|
||||
content, err := os.ReadFile(newFile)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, testData, content)
|
||||
})
|
||||
}
|
||||
|
||||
// TestConcurrentAccess tests concurrent access patterns
|
||||
func (s *POSIXComplianceTestSuite) TestConcurrentAccess(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("ConcurrentReads", func(t *testing.T) {
|
||||
testFile := filepath.Join(mountPoint, "concurrent_read.txt")
|
||||
testData := []byte("concurrent read test data")
|
||||
|
||||
// Create test file
|
||||
err := os.WriteFile(testFile, testData, 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Launch multiple concurrent readers
|
||||
const numReaders = 10
|
||||
results := make(chan error, numReaders)
|
||||
|
||||
for i := 0; i < numReaders; i++ {
|
||||
go func() {
|
||||
content, err := os.ReadFile(testFile)
|
||||
if err != nil {
|
||||
results <- err
|
||||
return
|
||||
}
|
||||
if string(content) != string(testData) {
|
||||
results <- fmt.Errorf("content mismatch")
|
||||
return
|
||||
}
|
||||
results <- nil
|
||||
}()
|
||||
}
|
||||
|
||||
// Check all readers succeeded
|
||||
for i := 0; i < numReaders; i++ {
|
||||
err := <-results
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ConcurrentFileCreations", func(t *testing.T) {
|
||||
testFile := filepath.Join(mountPoint, "concurrent_write.txt")
|
||||
|
||||
// Launch multiple concurrent writers
|
||||
const numWriters = 5
|
||||
results := make(chan error, numWriters)
|
||||
|
||||
for i := 0; i < numWriters; i++ {
|
||||
go func(id int) {
|
||||
content := fmt.Sprintf("writer %d data", id)
|
||||
err := os.WriteFile(fmt.Sprintf("%s_%d", testFile, id), []byte(content), 0644)
|
||||
results <- err
|
||||
}(i)
|
||||
}
|
||||
|
||||
// Check all writers succeeded
|
||||
for i := 0; i < numWriters; i++ {
|
||||
err := <-results
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// Verify all files were created
|
||||
for i := 0; i < numWriters; i++ {
|
||||
fileName := fmt.Sprintf("%s_%d", testFile, i)
|
||||
_, err := os.Stat(fileName)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestErrorHandling tests POSIX error handling compliance
|
||||
func (s *POSIXComplianceTestSuite) TestErrorHandling(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("AccessNonexistentFile", func(t *testing.T) {
|
||||
nonexistentFile := filepath.Join(mountPoint, "does_not_exist.txt")
|
||||
|
||||
// Reading nonexistent file should return ENOENT
|
||||
_, err := os.ReadFile(nonexistentFile)
|
||||
require.Error(t, err)
|
||||
require.True(t, os.IsNotExist(err))
|
||||
})
|
||||
|
||||
t.Run("CreateFileInNonexistentDirectory", func(t *testing.T) {
|
||||
fileInNonexistentDir := filepath.Join(mountPoint, "nonexistent_dir", "file.txt")
|
||||
|
||||
// Creating file in nonexistent directory should fail
|
||||
err := os.WriteFile(fileInNonexistentDir, []byte("test"), 0644)
|
||||
require.Error(t, err)
|
||||
require.True(t, os.IsNotExist(err))
|
||||
})
|
||||
|
||||
t.Run("InvalidFileDescriptor", func(t *testing.T) {
|
||||
// Using an invalid file descriptor should return appropriate error
|
||||
buffer := make([]byte, 10)
|
||||
_, err := syscall.Read(999, buffer) // 999 is likely an invalid fd
|
||||
require.Error(t, err)
|
||||
require.Equal(t, syscall.EBADF, err)
|
||||
})
|
||||
|
||||
t.Run("PermissionDenied", func(t *testing.T) {
|
||||
if os.Geteuid() == 0 {
|
||||
t.Skip("running as root: mode bits are not enforced")
|
||||
}
|
||||
testFile := filepath.Join(mountPoint, "readonly.txt")
|
||||
|
||||
// Create read-only file
|
||||
err := os.WriteFile(testFile, []byte("readonly"), 0444)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Attempting to write to read-only file should fail
|
||||
err = os.WriteFile(testFile, []byte("modified"), 0444)
|
||||
require.Error(t, err)
|
||||
require.True(t, os.IsPermission(err))
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,758 @@
|
||||
//go:build !windows
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// POSIXExtendedTestSuite provides additional POSIX compliance tests
|
||||
// covering extended attributes, file locking, and advanced features.
|
||||
//
|
||||
// NOTE: Some tests in this suite may be skipped or may have different behavior
|
||||
// depending on the FUSE implementation and platform support. This is expected
|
||||
// behavior for comprehensive POSIX compliance testing.
|
||||
//
|
||||
// See POSIX_IMPLEMENTATION_ROADMAP.md for a comprehensive plan to implement
|
||||
// these missing features.
|
||||
type POSIXExtendedTestSuite struct {
|
||||
framework *FuseTestFramework
|
||||
t *testing.T
|
||||
}
|
||||
|
||||
// ErrorCollector helps collect multiple test errors without stopping execution
|
||||
type ErrorCollector struct {
|
||||
errors []string
|
||||
t *testing.T
|
||||
}
|
||||
|
||||
// NewErrorCollector creates a new error collector
|
||||
func NewErrorCollector(t *testing.T) *ErrorCollector {
|
||||
return &ErrorCollector{
|
||||
errors: make([]string, 0),
|
||||
t: t,
|
||||
}
|
||||
}
|
||||
|
||||
// Add adds an error to the collection
|
||||
func (ec *ErrorCollector) Add(format string, args ...interface{}) {
|
||||
ec.errors = append(ec.errors, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
// Check checks a condition and adds an error if it fails
|
||||
func (ec *ErrorCollector) Check(condition bool, format string, args ...interface{}) {
|
||||
if !condition {
|
||||
ec.Add(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// CheckError checks if an error is nil and adds it if not
|
||||
func (ec *ErrorCollector) CheckError(err error, format string, args ...interface{}) {
|
||||
if err != nil {
|
||||
ec.Add(format+": %v", append(args, err)...)
|
||||
}
|
||||
}
|
||||
|
||||
// HasErrors returns true if any errors were collected
|
||||
func (ec *ErrorCollector) HasErrors() bool {
|
||||
return len(ec.errors) > 0
|
||||
}
|
||||
|
||||
// Report reports all collected errors to the test
|
||||
func (ec *ErrorCollector) Report() {
|
||||
if len(ec.errors) > 0 {
|
||||
ec.t.Errorf("Test completed with %d errors:\n%s", len(ec.errors), strings.Join(ec.errors, "\n"))
|
||||
}
|
||||
}
|
||||
|
||||
// NewPOSIXExtendedTestSuite creates a new extended POSIX compliance test suite
|
||||
func NewPOSIXExtendedTestSuite(t *testing.T, framework *FuseTestFramework) *POSIXExtendedTestSuite {
|
||||
return &POSIXExtendedTestSuite{
|
||||
framework: framework,
|
||||
t: t,
|
||||
}
|
||||
}
|
||||
|
||||
// TestPOSIXExtended runs extended POSIX compliance tests
|
||||
func TestPOSIXExtended(t *testing.T) {
|
||||
config := DefaultTestConfig()
|
||||
config.EnableDebug = true
|
||||
config.MountOptions = []string{"-allowOthers", "-nonempty"}
|
||||
|
||||
framework := NewFuseTestFramework(t, config)
|
||||
defer framework.Cleanup()
|
||||
|
||||
// Setup framework with better error handling
|
||||
err := framework.Setup(config)
|
||||
if err != nil {
|
||||
t.Logf("Framework setup failed: %v", err)
|
||||
t.Skip("Skipping extended tests due to framework setup failure")
|
||||
return
|
||||
}
|
||||
|
||||
// Verify framework is working
|
||||
mountPoint := framework.GetMountPoint()
|
||||
if mountPoint == "" {
|
||||
t.Log("Framework mount point is empty")
|
||||
t.Skip("Skipping extended tests due to invalid mount point")
|
||||
return
|
||||
}
|
||||
|
||||
suite := NewPOSIXExtendedTestSuite(t, framework)
|
||||
|
||||
// Run extended POSIX compliance test categories
|
||||
// Use a resilient approach that continues even if individual tests fail
|
||||
testCategories := []struct {
|
||||
name string
|
||||
fn func(*testing.T)
|
||||
}{
|
||||
{"ExtendedAttributes", suite.TestExtendedAttributes},
|
||||
{"FileLocking", suite.TestFileLocking},
|
||||
{"AdvancedIO", suite.TestAdvancedIO},
|
||||
{"SparseFiles", suite.TestSparseFiles},
|
||||
{"LargeFiles", suite.TestLargeFiles},
|
||||
{"MMap", suite.TestMemoryMapping},
|
||||
{"DirectIO", suite.TestDirectIO},
|
||||
{"FileSealing", suite.TestFileSealing},
|
||||
{"Fallocate", suite.TestFallocate},
|
||||
{"Sendfile", suite.TestSendfile},
|
||||
}
|
||||
|
||||
// Run all test categories and collect results
|
||||
var failedCategories []string
|
||||
for _, category := range testCategories {
|
||||
t.Run(category.name, func(subT *testing.T) {
|
||||
// Capture panics and convert them to test failures
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
subT.Errorf("Test category %s panicked: %v", category.name, r)
|
||||
failedCategories = append(failedCategories, category.name)
|
||||
}
|
||||
}()
|
||||
|
||||
category.fn(subT)
|
||||
if subT.Failed() {
|
||||
failedCategories = append(failedCategories, category.name)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Report overall results
|
||||
if len(failedCategories) > 0 {
|
||||
t.Logf("Extended POSIX tests completed. Failed categories: %v", failedCategories)
|
||||
} else {
|
||||
t.Logf("All extended POSIX test categories passed!")
|
||||
}
|
||||
}
|
||||
|
||||
// TestExtendedAttributes tests POSIX extended attribute support
|
||||
func (s *POSIXExtendedTestSuite) TestExtendedAttributes(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("SetAndGetXattr", func(t *testing.T) {
|
||||
if !isXattrSupported() {
|
||||
t.Skip("Extended attributes not supported on this platform")
|
||||
return
|
||||
}
|
||||
|
||||
testFile := filepath.Join(mountPoint, "xattr_test.txt")
|
||||
|
||||
// Create test file
|
||||
err := os.WriteFile(testFile, []byte("xattr test"), 0644)
|
||||
if !assert.NoError(t, err, "Failed to create test file") {
|
||||
return
|
||||
}
|
||||
|
||||
// Set extended attribute
|
||||
attrName := "user.test_attr"
|
||||
attrValue := []byte("test_value")
|
||||
err = setXattr(testFile, attrName, attrValue, 0)
|
||||
assert.NoError(t, err, "Failed to set extended attribute")
|
||||
|
||||
// Verify attribute was set
|
||||
readValue := make([]byte, 256)
|
||||
size, err := getXattr(testFile, attrName, readValue)
|
||||
assert.NoError(t, err, "Failed to get extended attribute")
|
||||
assert.Equal(t, len(attrValue), size, "Extended attribute size mismatch")
|
||||
assert.Equal(t, attrValue, readValue[:size], "Extended attribute value mismatch")
|
||||
})
|
||||
|
||||
t.Run("ListXattrs", func(t *testing.T) {
|
||||
if !isXattrSupported() {
|
||||
t.Skip("Extended attributes not supported on this platform")
|
||||
return
|
||||
}
|
||||
|
||||
testFile := filepath.Join(mountPoint, "xattr_list_test.txt")
|
||||
|
||||
// Create test file
|
||||
err := os.WriteFile(testFile, []byte("list xattr test"), 0644)
|
||||
if !assert.NoError(t, err, "Failed to create test file") {
|
||||
return
|
||||
}
|
||||
|
||||
// Set multiple extended attributes
|
||||
attrs := map[string][]byte{
|
||||
"user.attr1": []byte("value1"),
|
||||
"user.attr2": []byte("value2"),
|
||||
"user.attr3": []byte("value3"),
|
||||
}
|
||||
|
||||
for name, value := range attrs {
|
||||
err = setXattr(testFile, name, value, 0)
|
||||
assert.NoError(t, err, "Failed to set extended attribute %s", name)
|
||||
}
|
||||
|
||||
// List all attributes
|
||||
listBuf := make([]byte, 1024)
|
||||
size, err := listXattr(testFile, listBuf)
|
||||
assert.NoError(t, err, "Failed to list extended attributes")
|
||||
assert.Greater(t, size, 0, "No extended attributes found")
|
||||
|
||||
// Parse the null-separated list and verify attributes
|
||||
if size > 0 {
|
||||
attrList := parseXattrList(listBuf[:size])
|
||||
expectedAttrs := []string{"user.attr1", "user.attr2", "user.attr3"}
|
||||
for _, expectedAttr := range expectedAttrs {
|
||||
assert.Contains(t, attrList, expectedAttr, "Expected attribute %s should be in the list", expectedAttr)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("RemoveXattr", func(t *testing.T) {
|
||||
if !isXattrSupported() {
|
||||
t.Skip("Extended attributes not supported on this platform")
|
||||
return
|
||||
}
|
||||
|
||||
testFile := filepath.Join(mountPoint, "xattr_remove_test.txt")
|
||||
|
||||
// Create test file
|
||||
err := os.WriteFile(testFile, []byte("remove xattr test"), 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Set extended attribute
|
||||
attrName := "user.remove_test"
|
||||
attrValue := []byte("to_be_removed")
|
||||
err = setXattr(testFile, attrName, attrValue, 0)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify attribute exists
|
||||
readValue := make([]byte, 256)
|
||||
size, err := getXattr(testFile, attrName, readValue)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(attrValue), size)
|
||||
|
||||
// Remove the attribute
|
||||
err = removeXattr(testFile, attrName)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify attribute is gone
|
||||
_, err = getXattr(testFile, attrName, readValue)
|
||||
require.Error(t, err) // Should fail with ENODATA or similar
|
||||
})
|
||||
}
|
||||
|
||||
// TestFileLocking tests POSIX file locking mechanisms
|
||||
// Note: File locking behavior may vary between FUSE implementations.
|
||||
// Some implementations may not enforce locks between file descriptors
|
||||
// from the same process, which is a known limitation.
|
||||
func (s *POSIXExtendedTestSuite) TestFileLocking(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("AdvisoryLocking", func(t *testing.T) {
|
||||
testFile := filepath.Join(mountPoint, "lock_test.txt")
|
||||
|
||||
// Create test file
|
||||
err := os.WriteFile(testFile, []byte("locking test"), 0644)
|
||||
if !assert.NoError(t, err, "Failed to create test file") {
|
||||
return
|
||||
}
|
||||
|
||||
// Open file
|
||||
file, err := os.OpenFile(testFile, os.O_RDWR, 0644)
|
||||
if !assert.NoError(t, err, "Failed to open test file") {
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Apply exclusive lock
|
||||
flock := syscall.Flock_t{
|
||||
Type: syscall.F_WRLCK,
|
||||
Whence: 0,
|
||||
Start: 0,
|
||||
Len: 0, // Lock entire file
|
||||
}
|
||||
|
||||
err = syscall.FcntlFlock(file.Fd(), syscall.F_SETLK, &flock)
|
||||
assert.NoError(t, err, "Failed to acquire exclusive lock")
|
||||
|
||||
// Try to lock from another file descriptor (should fail)
|
||||
file2, err := os.OpenFile(testFile, os.O_RDWR, 0644)
|
||||
if !assert.NoError(t, err, "Failed to open second file descriptor") {
|
||||
return
|
||||
}
|
||||
defer file2.Close()
|
||||
|
||||
flock2 := syscall.Flock_t{
|
||||
Type: syscall.F_WRLCK,
|
||||
Whence: 0,
|
||||
Start: 0,
|
||||
Len: 0,
|
||||
}
|
||||
|
||||
// Note: Some FUSE implementations may not enforce file locking between file descriptors
|
||||
// from the same process. This is a known limitation in some FUSE implementations.
|
||||
err = syscall.FcntlFlock(file2.Fd(), syscall.F_SETLK, &flock2)
|
||||
if err == syscall.EAGAIN {
|
||||
// Lock was properly enforced
|
||||
t.Log("File locking properly enforced between file descriptors")
|
||||
} else if err == nil {
|
||||
// Lock was not enforced (common in some FUSE implementations)
|
||||
t.Log("File locking not enforced between file descriptors from same process (FUSE limitation)")
|
||||
} else {
|
||||
// Some other error occurred
|
||||
t.Logf("File locking attempt resulted in error: %v", err)
|
||||
}
|
||||
|
||||
// Log the actual error for debugging
|
||||
t.Logf("File locking test completed with result: %v", err)
|
||||
|
||||
// Release lock
|
||||
flock.Type = syscall.F_UNLCK
|
||||
err = syscall.FcntlFlock(file.Fd(), syscall.F_SETLK, &flock)
|
||||
assert.NoError(t, err, "Failed to release lock")
|
||||
|
||||
// Now second lock should succeed
|
||||
err = syscall.FcntlFlock(file2.Fd(), syscall.F_SETLK, &flock2)
|
||||
assert.NoError(t, err, "Failed to acquire lock after release")
|
||||
})
|
||||
|
||||
t.Run("SharedLocking", func(t *testing.T) {
|
||||
testFile := filepath.Join(mountPoint, "shared_lock_test.txt")
|
||||
|
||||
// Create test file
|
||||
err := os.WriteFile(testFile, []byte("shared locking test"), 0644)
|
||||
if !assert.NoError(t, err, "Failed to create test file") {
|
||||
return
|
||||
}
|
||||
|
||||
// Open file for reading
|
||||
file1, err := os.Open(testFile)
|
||||
if !assert.NoError(t, err, "Failed to open first file descriptor") {
|
||||
return
|
||||
}
|
||||
defer file1.Close()
|
||||
|
||||
file2, err := os.Open(testFile)
|
||||
if !assert.NoError(t, err, "Failed to open second file descriptor") {
|
||||
return
|
||||
}
|
||||
defer file2.Close()
|
||||
|
||||
// Apply shared locks (should both succeed)
|
||||
flock1 := syscall.Flock_t{
|
||||
Type: syscall.F_RDLCK,
|
||||
Whence: 0,
|
||||
Start: 0,
|
||||
Len: 0,
|
||||
}
|
||||
|
||||
flock2 := syscall.Flock_t{
|
||||
Type: syscall.F_RDLCK,
|
||||
Whence: 0,
|
||||
Start: 0,
|
||||
Len: 0,
|
||||
}
|
||||
|
||||
err = syscall.FcntlFlock(file1.Fd(), syscall.F_SETLK, &flock1)
|
||||
assert.NoError(t, err, "Failed to acquire first shared lock")
|
||||
|
||||
err = syscall.FcntlFlock(file2.Fd(), syscall.F_SETLK, &flock2)
|
||||
assert.NoError(t, err, "Failed to acquire second shared lock")
|
||||
})
|
||||
}
|
||||
|
||||
// TestAdvancedIO tests advanced I/O operations
|
||||
// Note: Vectored I/O support may vary between FUSE implementations.
|
||||
// Some implementations may not properly support readv/writev operations.
|
||||
func (s *POSIXExtendedTestSuite) TestAdvancedIO(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("ReadWriteV", func(t *testing.T) {
|
||||
if !isVectoredIOSupported() {
|
||||
t.Skip("Vectored I/O not supported on this platform")
|
||||
return
|
||||
}
|
||||
|
||||
testFile := filepath.Join(mountPoint, "readwritev_test.txt")
|
||||
|
||||
// Create file
|
||||
fd, err := syscall.Open(testFile, syscall.O_CREAT|syscall.O_RDWR, 0644)
|
||||
if !assert.NoError(t, err, "Failed to create test file") {
|
||||
return
|
||||
}
|
||||
defer syscall.Close(fd)
|
||||
|
||||
// Prepare test data in multiple buffers
|
||||
writeBuffers := [][]byte{
|
||||
[]byte("Hello "),
|
||||
[]byte("vectored "),
|
||||
[]byte("I/O "),
|
||||
[]byte("world!"),
|
||||
}
|
||||
|
||||
// Write using writev
|
||||
writeIOVs := makeIOVecs(writeBuffers)
|
||||
t.Logf("Attempting vectored I/O write with %d buffers", len(writeIOVs))
|
||||
totalWritten, err := writevFile(fd, writeIOVs)
|
||||
if err != nil {
|
||||
// Some FUSE implementations may not support vectored I/O properly
|
||||
t.Logf("Vectored I/O write failed: %v - this may be a FUSE limitation", err)
|
||||
t.Skip("Vectored I/O not properly supported by this FUSE implementation")
|
||||
return
|
||||
}
|
||||
|
||||
expectedTotal := 0
|
||||
for _, buf := range writeBuffers {
|
||||
expectedTotal += len(buf)
|
||||
}
|
||||
assert.Equal(t, expectedTotal, totalWritten, "Vectored write size mismatch")
|
||||
|
||||
// Seek back to beginning
|
||||
_, err = syscall.Seek(fd, 0, 0)
|
||||
if !assert.NoError(t, err, "Failed to seek to beginning") {
|
||||
return
|
||||
}
|
||||
|
||||
// Read using readv into multiple buffers
|
||||
readBuffers := [][]byte{
|
||||
make([]byte, 6), // "Hello "
|
||||
make([]byte, 9), // "vectored "
|
||||
make([]byte, 4), // "I/O "
|
||||
make([]byte, 6), // "world!"
|
||||
}
|
||||
|
||||
readIOVs := makeIOVecs(readBuffers)
|
||||
t.Logf("Attempting vectored I/O read with %d buffers", len(readIOVs))
|
||||
totalRead, err := readvFile(fd, readIOVs)
|
||||
if err != nil {
|
||||
t.Logf("Vectored I/O read failed: %v - this may be a FUSE limitation", err)
|
||||
t.Skip("Vectored I/O not properly supported by this FUSE implementation")
|
||||
return
|
||||
}
|
||||
assert.Equal(t, expectedTotal, totalRead, "Vectored read size mismatch")
|
||||
|
||||
// Verify data matches
|
||||
for i, expected := range writeBuffers {
|
||||
assert.Equal(t, expected, readBuffers[i], "Vectored I/O data mismatch at buffer %d", i)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// TestSparseFiles tests sparse file handling
|
||||
func (s *POSIXExtendedTestSuite) TestSparseFiles(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("CreateSparseFile", func(t *testing.T) {
|
||||
testFile := filepath.Join(mountPoint, "sparse_test.txt")
|
||||
|
||||
// Open file
|
||||
fd, err := syscall.Open(testFile, syscall.O_CREAT|syscall.O_RDWR, 0644)
|
||||
require.NoError(t, err)
|
||||
defer syscall.Close(fd)
|
||||
|
||||
// Create a sparse file by seeking beyond end and writing
|
||||
const sparseSize = 1024 * 1024 // 1MB
|
||||
_, err = syscall.Seek(fd, sparseSize, 0)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Write at the end
|
||||
endData := []byte("end")
|
||||
n, err := syscall.Write(fd, endData)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(endData), n)
|
||||
|
||||
// Verify file size
|
||||
stat, err := os.Stat(testFile)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(sparseSize+len(endData)), stat.Size())
|
||||
|
||||
// Read from the beginning (should be zeros)
|
||||
_, err = syscall.Seek(fd, 0, 0)
|
||||
require.NoError(t, err)
|
||||
|
||||
buffer := make([]byte, 100)
|
||||
n, err = syscall.Read(fd, buffer)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 100, n)
|
||||
|
||||
// Should be all zeros
|
||||
for _, b := range buffer {
|
||||
require.Equal(t, byte(0), b)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestLargeFiles tests large file handling (>2GB)
|
||||
func (s *POSIXExtendedTestSuite) TestLargeFiles(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("LargeFileOperations", func(t *testing.T) {
|
||||
testFile := filepath.Join(mountPoint, "large_file_test.txt")
|
||||
|
||||
// Open file
|
||||
fd, err := syscall.Open(testFile, syscall.O_CREAT|syscall.O_RDWR, 0644)
|
||||
require.NoError(t, err)
|
||||
defer syscall.Close(fd)
|
||||
|
||||
// Seek to position > 2GB
|
||||
const largeOffset = 3 * 1024 * 1024 * 1024 // 3GB
|
||||
pos, err := syscall.Seek(fd, largeOffset, 0)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(largeOffset), pos)
|
||||
|
||||
// Write at large offset
|
||||
testData := []byte("large file data")
|
||||
n, err := syscall.Write(fd, testData)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(testData), n)
|
||||
|
||||
// Read back from large offset
|
||||
_, err = syscall.Seek(fd, largeOffset, 0)
|
||||
require.NoError(t, err)
|
||||
|
||||
readBuffer := make([]byte, len(testData))
|
||||
n, err = syscall.Read(fd, readBuffer)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(testData), n)
|
||||
require.Equal(t, testData, readBuffer)
|
||||
})
|
||||
}
|
||||
|
||||
// TestMemoryMapping tests memory mapping functionality
|
||||
func (s *POSIXExtendedTestSuite) TestMemoryMapping(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("MmapFile", func(t *testing.T) {
|
||||
if !isMmapSupported() {
|
||||
t.Skip("Memory mapping not supported on this platform")
|
||||
return
|
||||
}
|
||||
|
||||
testFile := filepath.Join(mountPoint, "mmap_test.txt")
|
||||
testData := make([]byte, 4096)
|
||||
for i := range testData {
|
||||
testData[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
// Create test file
|
||||
err := os.WriteFile(testFile, testData, 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Open file for reading
|
||||
fd, err := syscall.Open(testFile, syscall.O_RDONLY, 0)
|
||||
require.NoError(t, err)
|
||||
defer syscall.Close(fd)
|
||||
|
||||
// Memory map the file
|
||||
mappedData, err := mmapFile(fd, 0, len(testData), PROT_READ, MAP_SHARED)
|
||||
require.NoError(t, err)
|
||||
defer munmapFile(mappedData)
|
||||
|
||||
// Verify mapped content matches original
|
||||
require.Equal(t, testData, mappedData)
|
||||
})
|
||||
|
||||
t.Run("MmapWrite", func(t *testing.T) {
|
||||
if !isMmapSupported() {
|
||||
t.Skip("Memory mapping not supported on this platform")
|
||||
return
|
||||
}
|
||||
|
||||
testFile := filepath.Join(mountPoint, "mmap_write_test.txt")
|
||||
size := 4096
|
||||
|
||||
// Create empty file of specific size
|
||||
fd, err := syscall.Open(testFile, syscall.O_CREAT|syscall.O_RDWR, 0644)
|
||||
require.NoError(t, err)
|
||||
defer syscall.Close(fd)
|
||||
|
||||
err = syscall.Ftruncate(fd, int64(size))
|
||||
require.NoError(t, err)
|
||||
|
||||
// Memory map the file for writing
|
||||
mappedData, err := mmapFile(fd, 0, size, PROT_READ|PROT_WRITE, MAP_SHARED)
|
||||
require.NoError(t, err)
|
||||
defer munmapFile(mappedData)
|
||||
|
||||
// Write test pattern to mapped memory
|
||||
testPattern := []byte("Hello, mmap world!")
|
||||
copy(mappedData, testPattern)
|
||||
|
||||
// Sync changes to disk
|
||||
err = msyncFile(mappedData, MS_SYNC)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify changes were written by reading file directly
|
||||
readData, err := os.ReadFile(testFile)
|
||||
require.NoError(t, err)
|
||||
require.True(t, len(readData) >= len(testPattern))
|
||||
require.Equal(t, testPattern, readData[:len(testPattern)])
|
||||
})
|
||||
}
|
||||
|
||||
// TestDirectIO tests direct I/O operations
|
||||
func (s *POSIXExtendedTestSuite) TestDirectIO(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("DirectIO", func(t *testing.T) {
|
||||
if !isDirectIOSupported() {
|
||||
t.Skip("Direct I/O not supported on this platform")
|
||||
return
|
||||
}
|
||||
|
||||
testFile := filepath.Join(mountPoint, "directio_test.txt")
|
||||
|
||||
// Open file with direct I/O
|
||||
fd, err := openDirectIO(testFile, syscall.O_CREAT|syscall.O_RDWR, 0644)
|
||||
require.NoError(t, err)
|
||||
defer syscall.Close(fd)
|
||||
|
||||
// For direct I/O, data must be aligned to sector boundaries
|
||||
// Use 4KB aligned buffer (common sector size)
|
||||
blockSize := 4096
|
||||
testData := make([]byte, blockSize)
|
||||
for i := range testData {
|
||||
testData[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
// Write data using direct I/O
|
||||
n, err := syscall.Write(fd, testData)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(testData), n)
|
||||
|
||||
// Seek back to beginning
|
||||
_, err = syscall.Seek(fd, 0, 0)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Read data using direct I/O
|
||||
readData := make([]byte, blockSize)
|
||||
n, err = syscall.Read(fd, readData)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(testData), n)
|
||||
require.Equal(t, testData, readData)
|
||||
})
|
||||
}
|
||||
|
||||
// TestFileSealing tests file sealing mechanisms (Linux-specific)
|
||||
func (s *POSIXExtendedTestSuite) TestFileSealing(t *testing.T) {
|
||||
// This test is Linux-specific and may not be applicable to all systems
|
||||
t.Skip("File sealing tests require Linux-specific features")
|
||||
}
|
||||
|
||||
// TestFallocate tests file preallocation
|
||||
func (s *POSIXExtendedTestSuite) TestFallocate(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("FallocateSpace", func(t *testing.T) {
|
||||
if !isFallocateSupported() {
|
||||
t.Skip("fallocate not supported on this platform")
|
||||
return
|
||||
}
|
||||
|
||||
testFile := filepath.Join(mountPoint, "fallocate_test.txt")
|
||||
|
||||
// Open file
|
||||
fd, err := syscall.Open(testFile, syscall.O_CREAT|syscall.O_RDWR, 0644)
|
||||
require.NoError(t, err)
|
||||
defer syscall.Close(fd)
|
||||
|
||||
// Preallocate 1MB of space
|
||||
allocSize := int64(1024 * 1024)
|
||||
err = fallocateFile(fd, 0, 0, allocSize)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify file size was extended
|
||||
var stat syscall.Stat_t
|
||||
err = syscall.Fstat(fd, &stat)
|
||||
require.NoError(t, err)
|
||||
require.GreaterOrEqual(t, stat.Size, allocSize)
|
||||
|
||||
// Write some data and verify it works
|
||||
testData := []byte("fallocate test data")
|
||||
n, err := syscall.Write(fd, testData)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(testData), n)
|
||||
})
|
||||
}
|
||||
|
||||
// TestSendfile tests zero-copy file transfer
|
||||
func (s *POSIXExtendedTestSuite) TestSendfile(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
t.Run("SendfileCopy", func(t *testing.T) {
|
||||
sourceFile := filepath.Join(mountPoint, "sendfile_source.txt")
|
||||
targetFile := filepath.Join(mountPoint, "sendfile_target.txt")
|
||||
|
||||
testData := make([]byte, 8192)
|
||||
for i := range testData {
|
||||
testData[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
// Create source file
|
||||
err := os.WriteFile(sourceFile, testData, 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Open source for reading
|
||||
srcFd, err := syscall.Open(sourceFile, syscall.O_RDONLY, 0)
|
||||
require.NoError(t, err)
|
||||
defer syscall.Close(srcFd)
|
||||
|
||||
// Create target file
|
||||
dstFd, err := syscall.Open(targetFile, syscall.O_CREAT|syscall.O_WRONLY, 0644)
|
||||
require.NoError(t, err)
|
||||
defer syscall.Close(dstFd)
|
||||
|
||||
// Sendfile test
|
||||
if !isSendfileSupported() {
|
||||
t.Skip("sendfile not supported on this platform")
|
||||
return
|
||||
}
|
||||
|
||||
// Use sendfile to copy data
|
||||
var offset int64 = 0
|
||||
transferred, err := sendfileTransfer(dstFd, srcFd, &offset, len(testData))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(testData), transferred)
|
||||
|
||||
// Verify copy
|
||||
copiedData, err := os.ReadFile(targetFile)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, testData, copiedData)
|
||||
})
|
||||
}
|
||||
|
||||
// Helper function to parse null-separated xattr list
|
||||
func parseXattrList(data []byte) []string {
|
||||
var attrs []string
|
||||
start := 0
|
||||
for i, b := range data {
|
||||
if b == 0 {
|
||||
if i > start {
|
||||
attrs = append(attrs, string(data[start:i]))
|
||||
}
|
||||
start = i + 1
|
||||
}
|
||||
}
|
||||
return attrs
|
||||
}
|
||||
@@ -0,0 +1,576 @@
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// ExternalPOSIXTestSuite manages integration with external POSIX test suites
|
||||
type ExternalPOSIXTestSuite struct {
|
||||
framework *FuseTestFramework
|
||||
t *testing.T
|
||||
workDir string
|
||||
}
|
||||
|
||||
// NewExternalPOSIXTestSuite creates a new external POSIX test suite runner
|
||||
func NewExternalPOSIXTestSuite(t *testing.T, framework *FuseTestFramework) *ExternalPOSIXTestSuite {
|
||||
workDir, err := os.MkdirTemp("", "posix_external_tests_")
|
||||
require.NoError(t, err)
|
||||
|
||||
return &ExternalPOSIXTestSuite{
|
||||
framework: framework,
|
||||
t: t,
|
||||
workDir: workDir,
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup removes temporary test directories
|
||||
func (s *ExternalPOSIXTestSuite) Cleanup() {
|
||||
os.RemoveAll(s.workDir)
|
||||
}
|
||||
|
||||
// TestExternalPOSIXSuites runs integration tests with external POSIX test suites
|
||||
func TestExternalPOSIXSuites(t *testing.T) {
|
||||
config := DefaultTestConfig()
|
||||
config.EnableDebug = true
|
||||
config.MountOptions = []string{"-allowOthers", "-nonempty"}
|
||||
|
||||
framework := NewFuseTestFramework(t, config)
|
||||
defer framework.Cleanup()
|
||||
require.NoError(t, framework.Setup(config))
|
||||
|
||||
suite := NewExternalPOSIXTestSuite(t, framework)
|
||||
defer suite.Cleanup()
|
||||
|
||||
// Run various external POSIX test suites
|
||||
t.Run("PjdFsTest", suite.TestPjdFsTest)
|
||||
t.Run("NFSTestPOSIX", suite.TestNFSTestPOSIX)
|
||||
t.Run("LitmusTests", suite.TestLitmusTests)
|
||||
t.Run("CustomPOSIXTests", suite.TestCustomPOSIXTests)
|
||||
}
|
||||
|
||||
// TestPjdFsTest runs the comprehensive pjdfstest POSIX filesystem test suite
|
||||
func (s *ExternalPOSIXTestSuite) TestPjdFsTest(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
// Create test directory within mount point
|
||||
testDir := filepath.Join(mountPoint, "pjdfstest")
|
||||
err := os.MkdirAll(testDir, 0755)
|
||||
require.NoError(t, err)
|
||||
|
||||
// List of critical POSIX operations to test
|
||||
pjdTests := []struct {
|
||||
name string
|
||||
testPath string
|
||||
critical bool
|
||||
}{
|
||||
{"chflags", "tests/chflags", false},
|
||||
{"chmod", "tests/chmod", true},
|
||||
{"chown", "tests/chown", true},
|
||||
{"create", "tests/create", true},
|
||||
{"link", "tests/link", true},
|
||||
{"mkdir", "tests/mkdir", true},
|
||||
{"mkfifo", "tests/mkfifo", false},
|
||||
{"mknod", "tests/mknod", false},
|
||||
{"open", "tests/open", true},
|
||||
{"rename", "tests/rename", true},
|
||||
{"rmdir", "tests/rmdir", true},
|
||||
{"symlink", "tests/symlink", true},
|
||||
{"truncate", "tests/truncate", true},
|
||||
{"unlink", "tests/unlink", true},
|
||||
}
|
||||
|
||||
// Prefer the suite that `make setup-pjdfstest` builds under
|
||||
// external_tools/ before cloning a second copy into the work dir
|
||||
pjdDir := s.findPjdDir()
|
||||
if pjdDir == "" {
|
||||
pjdDir = filepath.Join(s.workDir, "pjdfstest")
|
||||
if err := s.setupPjdFsTest(pjdDir); err != nil {
|
||||
if os.Getenv("POSIX_REQUIRE_EXTERNAL_TOOLS") == "true" {
|
||||
t.Fatalf("pjdfstest is required for comprehensive POSIX compliance testing: %v", err)
|
||||
}
|
||||
t.Skipf("pjdfstest not found and setup failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Run each test category
|
||||
for _, test := range pjdTests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
s.runPjdTest(t, pjdDir, test.testPath, testDir, test.critical)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestNFSTestPOSIX runs nfstest_posix for POSIX API verification
|
||||
func (s *ExternalPOSIXTestSuite) TestNFSTestPOSIX(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
// Check if nfstest_posix is available
|
||||
_, err := exec.LookPath("nfstest_posix")
|
||||
if err != nil {
|
||||
// For comprehensive POSIX compliance testing, external tools should be mandatory
|
||||
if os.Getenv("POSIX_REQUIRE_EXTERNAL_TOOLS") == "true" {
|
||||
t.Fatalf("nfstest_posix is required for comprehensive POSIX compliance testing but not found. Install via: pip install nfstest")
|
||||
}
|
||||
t.Skip("nfstest_posix not found. Install via: pip install nfstest")
|
||||
}
|
||||
|
||||
testDir := filepath.Join(mountPoint, "nfstest")
|
||||
err = os.MkdirAll(testDir, 0755)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Run nfstest_posix with comprehensive API testing
|
||||
cmd := exec.Command("nfstest_posix",
|
||||
"--path", testDir,
|
||||
"--verbose",
|
||||
"--createlog",
|
||||
"--runid", fmt.Sprintf("seaweedfs_%d", time.Now().Unix()),
|
||||
)
|
||||
|
||||
output, err := cmd.CombinedOutput()
|
||||
t.Logf("nfstest_posix output:\n%s", string(output))
|
||||
|
||||
if err != nil {
|
||||
t.Logf("nfstest_posix failed: %v", err)
|
||||
// Don't fail the test completely, just log the failure
|
||||
}
|
||||
}
|
||||
|
||||
// TestLitmusTests runs focused POSIX compliance litmus tests
|
||||
func (s *ExternalPOSIXTestSuite) TestLitmusTests(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
// Create litmus test scripts for critical POSIX behaviors
|
||||
litmusTests := []struct {
|
||||
name string
|
||||
script string
|
||||
}{
|
||||
{
|
||||
name: "AtomicRename",
|
||||
script: `#!/bin/bash
|
||||
set -e
|
||||
cd "$1"
|
||||
echo "test data" > temp_file
|
||||
echo "original" > target_file
|
||||
mv temp_file target_file
|
||||
[ "$(cat target_file)" = "test data" ]
|
||||
echo "PASS: Atomic rename works"
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "LinkCount",
|
||||
script: `#!/bin/bash
|
||||
set -e
|
||||
cd "$1"
|
||||
echo "test" > original
|
||||
ln original hardlink
|
||||
if [[ "$(uname)" == "Darwin" ]]; then
|
||||
[ $(stat -f %l original) -eq 2 ]
|
||||
else
|
||||
[ $(stat -c %h original) -eq 2 ]
|
||||
fi
|
||||
rm hardlink
|
||||
if [[ "$(uname)" == "Darwin" ]]; then
|
||||
[ $(stat -f %l original) -eq 1 ]
|
||||
else
|
||||
[ $(stat -c %h original) -eq 1 ]
|
||||
fi
|
||||
echo "PASS: Hard link counting works"
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "SymlinkCycles",
|
||||
script: `#!/bin/bash
|
||||
set -e
|
||||
cd "$1"
|
||||
ln -s link1 link2
|
||||
ln -s link2 link1
|
||||
# Try to access the file, which should fail due to too many symbolic links
|
||||
if cat link1 2>/dev/null; then
|
||||
echo "FAIL: Symlink cycle not detected"
|
||||
exit 1
|
||||
fi
|
||||
echo "PASS: Symlink cycle handling works"
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "ConcurrentCreate",
|
||||
script: `#!/bin/bash
|
||||
set -e
|
||||
cd "$1"
|
||||
for i in {1..10}; do
|
||||
(echo "process $i" > "file_$i") &
|
||||
done
|
||||
wait
|
||||
[ $(ls file_* | wc -l) -eq 10 ]
|
||||
echo "PASS: Concurrent file creation works"
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "DirectoryConsistency",
|
||||
script: `#!/bin/bash
|
||||
set -e
|
||||
cd "$1"
|
||||
mkdir testdir
|
||||
cd testdir
|
||||
touch file1 file2 file3
|
||||
cd ..
|
||||
entries=$(ls testdir | wc -l)
|
||||
[ $entries -eq 3 ]
|
||||
rmdir testdir 2>/dev/null && echo "FAIL: Non-empty directory removed" && exit 1
|
||||
rm testdir/*
|
||||
rmdir testdir
|
||||
echo "PASS: Directory consistency works"
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
||||
testDir := filepath.Join(mountPoint, "litmus")
|
||||
err := os.MkdirAll(testDir, 0755)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Run each litmus test
|
||||
for _, test := range litmusTests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
s.runLitmusTest(t, test.name, test.script, testDir)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCustomPOSIXTests runs custom POSIX compliance tests
|
||||
func (s *ExternalPOSIXTestSuite) TestCustomPOSIXTests(t *testing.T) {
|
||||
mountPoint := s.framework.GetMountPoint()
|
||||
|
||||
// Custom stress tests for POSIX compliance
|
||||
t.Run("StressRename", func(t *testing.T) {
|
||||
s.stressTestRename(t, mountPoint)
|
||||
})
|
||||
|
||||
t.Run("StressCreate", func(t *testing.T) {
|
||||
s.stressTestCreate(t, mountPoint)
|
||||
})
|
||||
|
||||
t.Run("StressDirectory", func(t *testing.T) {
|
||||
s.stressTestDirectory(t, mountPoint)
|
||||
})
|
||||
|
||||
t.Run("EdgeCases", func(t *testing.T) {
|
||||
s.testEdgeCases(t, mountPoint)
|
||||
})
|
||||
}
|
||||
|
||||
// findPjdDir returns a directory holding a built pjdfstest suite, or ""
|
||||
// when none is installed. The path is absolute because tests run with
|
||||
// cmd.Dir inside the mount.
|
||||
func (s *ExternalPOSIXTestSuite) findPjdDir() string {
|
||||
dir := filepath.Join("external_tools", "pjdfstest")
|
||||
if info, err := os.Stat(filepath.Join(dir, "pjdfstest")); err == nil && !info.IsDir() && info.Mode()&0111 != 0 {
|
||||
if abs, err := filepath.Abs(dir); err == nil {
|
||||
return abs
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// setupPjdFsTest downloads and sets up the pjdfstest suite
|
||||
func (s *ExternalPOSIXTestSuite) setupPjdFsTest(targetDir string) error {
|
||||
// Clone pjdfstest repository
|
||||
cmd := exec.Command("git", "clone", "https://github.com/pjd/pjdfstest.git", targetDir)
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("failed to clone pjdfstest: %w", err)
|
||||
}
|
||||
|
||||
// pjdfstest is autotools-based: the fresh clone has no Makefile until
|
||||
// configure runs
|
||||
for _, step := range [][]string{
|
||||
{"autoreconf", "-ifs"},
|
||||
{"./configure"},
|
||||
{"make"},
|
||||
} {
|
||||
cmd = exec.Command(step[0], step[1:]...)
|
||||
cmd.Dir = targetDir
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("pjdfstest %s: %w: %s", step[0], err, out)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// runPjdTest executes a specific pjdfstest
|
||||
func (s *ExternalPOSIXTestSuite) runPjdTest(t *testing.T, pjdDir, testPath, mountDir string, critical bool) {
|
||||
fullTestPath := filepath.Join(pjdDir, testPath)
|
||||
|
||||
// Check if test path exists
|
||||
if _, err := os.Stat(fullTestPath); os.IsNotExist(err) {
|
||||
t.Skipf("Test path does not exist: %s", fullTestPath)
|
||||
}
|
||||
|
||||
cmd := exec.Command("prove", "-r", fullTestPath)
|
||||
cmd.Dir = mountDir
|
||||
cmd.Env = append(os.Environ(), "FSTEST="+filepath.Join(pjdDir, "pjdfstest"))
|
||||
|
||||
output, err := cmd.CombinedOutput()
|
||||
t.Logf("pjdfstest %s output:\n%s", testPath, string(output))
|
||||
|
||||
if err != nil {
|
||||
if critical {
|
||||
t.Errorf("Critical pjdfstest failed: %s - %v", testPath, err)
|
||||
} else {
|
||||
t.Logf("Non-critical pjdfstest failed: %s - %v", testPath, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// runLitmusTest executes a litmus test script
|
||||
func (s *ExternalPOSIXTestSuite) runLitmusTest(t *testing.T, name, script, testDir string) {
|
||||
scriptPath := filepath.Join(s.workDir, name+".sh")
|
||||
|
||||
// Write script to file
|
||||
err := os.WriteFile(scriptPath, []byte(script), 0755)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create isolated test directory
|
||||
isolatedDir := filepath.Join(testDir, name)
|
||||
err = os.MkdirAll(isolatedDir, 0755)
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(isolatedDir)
|
||||
|
||||
// Execute script
|
||||
cmd := exec.Command("bash", scriptPath, isolatedDir)
|
||||
output, err := cmd.CombinedOutput()
|
||||
|
||||
t.Logf("Litmus test %s output:\n%s", name, string(output))
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Litmus test %s failed: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// stressTestRename tests rename operations under stress
|
||||
func (s *ExternalPOSIXTestSuite) stressTestRename(t *testing.T, mountPoint string) {
|
||||
testDir := filepath.Join(mountPoint, "stress_rename")
|
||||
err := os.MkdirAll(testDir, 0755)
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(testDir)
|
||||
|
||||
// Create files and rename them concurrently
|
||||
const numFiles = 100
|
||||
const numWorkers = 10
|
||||
|
||||
// Create initial files
|
||||
for i := 0; i < numFiles; i++ {
|
||||
fileName := filepath.Join(testDir, fmt.Sprintf("file_%d.txt", i))
|
||||
err := os.WriteFile(fileName, []byte(fmt.Sprintf("content_%d", i)), 0644)
|
||||
if !assert.NoError(t, err, "Failed to create initial file %d", i) {
|
||||
return // Skip test if setup fails
|
||||
}
|
||||
}
|
||||
|
||||
// Concurrent rename operations
|
||||
results := make(chan error, numWorkers)
|
||||
for w := 0; w < numWorkers; w++ {
|
||||
go func(worker int) {
|
||||
for i := worker; i < numFiles; i += numWorkers {
|
||||
oldName := filepath.Join(testDir, fmt.Sprintf("file_%d.txt", i))
|
||||
newName := filepath.Join(testDir, fmt.Sprintf("renamed_%d.txt", i))
|
||||
err := os.Rename(oldName, newName)
|
||||
if err != nil {
|
||||
results <- err
|
||||
return
|
||||
}
|
||||
}
|
||||
results <- nil
|
||||
}(w)
|
||||
}
|
||||
|
||||
// Wait for all workers and collect errors
|
||||
var errorCount int
|
||||
for w := 0; w < numWorkers; w++ {
|
||||
err := <-results
|
||||
if err != nil {
|
||||
assert.NoError(t, err, "Worker %d failed", w)
|
||||
errorCount++
|
||||
}
|
||||
}
|
||||
|
||||
// Verify all files were renamed (if no errors occurred)
|
||||
if errorCount == 0 {
|
||||
files, err := filepath.Glob(filepath.Join(testDir, "renamed_*.txt"))
|
||||
assert.NoError(t, err, "Failed to list renamed files")
|
||||
assert.Equal(t, numFiles, len(files), "Not all files were renamed successfully")
|
||||
}
|
||||
}
|
||||
|
||||
// stressTestCreate tests file creation under stress
|
||||
func (s *ExternalPOSIXTestSuite) stressTestCreate(t *testing.T, mountPoint string) {
|
||||
testDir := filepath.Join(mountPoint, "stress_create")
|
||||
err := os.MkdirAll(testDir, 0755)
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(testDir)
|
||||
|
||||
const numFiles = 1000
|
||||
const numWorkers = 20
|
||||
|
||||
results := make(chan error, numWorkers)
|
||||
|
||||
for w := 0; w < numWorkers; w++ {
|
||||
go func(worker int) {
|
||||
for i := worker; i < numFiles; i += numWorkers {
|
||||
fileName := filepath.Join(testDir, fmt.Sprintf("stress_%d_%d.txt", worker, i))
|
||||
err := os.WriteFile(fileName, []byte(fmt.Sprintf("data_%d_%d", worker, i)), 0644)
|
||||
if err != nil {
|
||||
results <- err
|
||||
return
|
||||
}
|
||||
}
|
||||
results <- nil
|
||||
}(w)
|
||||
}
|
||||
|
||||
// Wait for all workers
|
||||
for w := 0; w < numWorkers; w++ {
|
||||
err := <-results
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// Verify all files were created
|
||||
files, err := filepath.Glob(filepath.Join(testDir, "stress_*.txt"))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, numFiles, len(files))
|
||||
}
|
||||
|
||||
// stressTestDirectory tests directory operations under stress
|
||||
func (s *ExternalPOSIXTestSuite) stressTestDirectory(t *testing.T, mountPoint string) {
|
||||
testDir := filepath.Join(mountPoint, "stress_directory")
|
||||
err := os.MkdirAll(testDir, 0755)
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(testDir)
|
||||
|
||||
const numDirs = 500
|
||||
const numWorkers = 10
|
||||
|
||||
results := make(chan error, numWorkers)
|
||||
|
||||
// Create directories concurrently
|
||||
for w := 0; w < numWorkers; w++ {
|
||||
go func(worker int) {
|
||||
for i := worker; i < numDirs; i += numWorkers {
|
||||
dirName := filepath.Join(testDir, fmt.Sprintf("dir_%d_%d", worker, i))
|
||||
err := os.MkdirAll(dirName, 0755)
|
||||
if err != nil {
|
||||
results <- err
|
||||
return
|
||||
}
|
||||
|
||||
// Create a file in each directory
|
||||
fileName := filepath.Join(dirName, "test.txt")
|
||||
err = os.WriteFile(fileName, []byte("test"), 0644)
|
||||
if err != nil {
|
||||
results <- err
|
||||
return
|
||||
}
|
||||
}
|
||||
results <- nil
|
||||
}(w)
|
||||
}
|
||||
|
||||
// Wait for all workers
|
||||
for w := 0; w < numWorkers; w++ {
|
||||
err := <-results
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// Verify directory structure
|
||||
dirs, err := filepath.Glob(filepath.Join(testDir, "dir_*"))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, numDirs, len(dirs))
|
||||
}
|
||||
|
||||
// testEdgeCases tests various POSIX edge cases
|
||||
func (s *ExternalPOSIXTestSuite) testEdgeCases(t *testing.T, mountPoint string) {
|
||||
testDir := filepath.Join(mountPoint, "edge_cases")
|
||||
err := os.MkdirAll(testDir, 0755)
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(testDir)
|
||||
|
||||
t.Run("WriteToDirectoryAsFile", func(t *testing.T) {
|
||||
// Test writing to a directory as if it were a file (should fail)
|
||||
// Note: filepath.Join(testDir, "") returns testDir itself
|
||||
err := os.WriteFile(testDir, []byte("test"), 0644)
|
||||
require.Error(t, err)
|
||||
// Verify the error is specifically about the target being a directory
|
||||
var pathErr *os.PathError
|
||||
require.ErrorAs(t, err, &pathErr)
|
||||
require.Equal(t, syscall.EISDIR, pathErr.Err)
|
||||
})
|
||||
|
||||
t.Run("VeryLongFileName", func(t *testing.T) {
|
||||
// Test very long file names
|
||||
longName := strings.Repeat("a", 255) // NAME_MAX is typically 255
|
||||
longFile := filepath.Join(testDir, longName)
|
||||
|
||||
err := os.WriteFile(longFile, []byte("long name test"), 0644)
|
||||
// This might succeed or fail depending on filesystem limits
|
||||
if err == nil {
|
||||
defer os.Remove(longFile)
|
||||
t.Logf("Very long filename accepted")
|
||||
} else {
|
||||
t.Logf("Very long filename rejected: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("SpecialCharacters", func(t *testing.T) {
|
||||
// Test files with special characters
|
||||
specialFiles := []string{
|
||||
"file with spaces.txt",
|
||||
"file-with-dashes.txt",
|
||||
"file_with_underscores.txt",
|
||||
"file.with.dots.txt",
|
||||
}
|
||||
|
||||
for _, fileName := range specialFiles {
|
||||
filePath := filepath.Join(testDir, fileName)
|
||||
err := os.WriteFile(filePath, []byte("special char test"), 0644)
|
||||
require.NoError(t, err, "Failed to create file with special chars: %s", fileName)
|
||||
|
||||
// Verify we can read it back
|
||||
_, err = os.ReadFile(filePath)
|
||||
require.NoError(t, err, "Failed to read file with special chars: %s", fileName)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("DeepDirectoryNesting", func(t *testing.T) {
|
||||
// Test deep directory nesting
|
||||
deepPath := testDir
|
||||
for i := 0; i < 100; i++ {
|
||||
deepPath = filepath.Join(deepPath, fmt.Sprintf("level_%d", i))
|
||||
}
|
||||
|
||||
err := os.MkdirAll(deepPath, 0755)
|
||||
// This might fail due to PATH_MAX limits
|
||||
if err != nil {
|
||||
t.Logf("Deep directory nesting failed at some level: %v", err)
|
||||
} else {
|
||||
t.Logf("Deep directory nesting succeeded")
|
||||
|
||||
// Test creating a file in the deep path
|
||||
testFile := filepath.Join(deepPath, "deep_file.txt")
|
||||
err = os.WriteFile(testFile, []byte("deep test"), 0644)
|
||||
if err == nil {
|
||||
t.Logf("File creation in deep path succeeded")
|
||||
} else {
|
||||
t.Logf("File creation in deep path failed: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
//go:build darwin
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Sendfile support for macOS
|
||||
|
||||
func sendfileTransfer(outFd int, inFd int, offset *int64, count int) (int, error) {
|
||||
// macOS sendfile has different signature: sendfile(in_fd, out_fd, offset, len, hdtr, flags)
|
||||
var off int64
|
||||
if offset != nil {
|
||||
off = *offset
|
||||
}
|
||||
|
||||
length := int64(count)
|
||||
|
||||
_, _, errno := syscall.Syscall6(syscall.SYS_SENDFILE,
|
||||
uintptr(inFd), // input fd
|
||||
uintptr(outFd), // output fd
|
||||
uintptr(off), // offset
|
||||
uintptr(unsafe.Pointer(&length)), // length (in/out parameter)
|
||||
0, // hdtr (headers/trailers)
|
||||
0) // flags
|
||||
|
||||
if errno != 0 {
|
||||
return 0, errno
|
||||
}
|
||||
|
||||
// Update offset if provided
|
||||
if offset != nil {
|
||||
*offset += length
|
||||
}
|
||||
|
||||
return int(length), nil
|
||||
}
|
||||
|
||||
func isSendfileSupported() bool {
|
||||
// macOS sendfile(2) requires a socket destination; file-to-file copies
|
||||
// fail with ENOTSOCK.
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//go:build linux
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Sendfile support for Linux
|
||||
|
||||
func sendfileTransfer(outFd int, inFd int, offset *int64, count int) (int, error) {
|
||||
var offsetPtr uintptr
|
||||
if offset != nil {
|
||||
offsetPtr = uintptr(unsafe.Pointer(offset))
|
||||
}
|
||||
|
||||
n, _, errno := syscall.Syscall6(syscall.SYS_SENDFILE,
|
||||
uintptr(outFd),
|
||||
uintptr(inFd),
|
||||
offsetPtr,
|
||||
uintptr(count),
|
||||
0, 0)
|
||||
|
||||
if errno != 0 {
|
||||
return 0, errno
|
||||
}
|
||||
return int(n), nil
|
||||
}
|
||||
|
||||
func isSendfileSupported() bool {
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
//go:build !linux && !darwin
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Sendfile support for unsupported platforms
|
||||
|
||||
var ErrSendfileNotSupported = errors.New("sendfile not supported on this platform")
|
||||
|
||||
func sendfileTransfer(outFd int, inFd int, offset *int64, count int) (int, error) {
|
||||
return 0, ErrSendfileNotSupported
|
||||
}
|
||||
|
||||
func isSendfileSupported() bool {
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
//go:build unix
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Vectored I/O support for Unix-like systems
|
||||
|
||||
// IOVec represents an I/O vector for readv/writev operations
|
||||
type IOVec struct {
|
||||
Base *byte
|
||||
Len uint64
|
||||
}
|
||||
|
||||
func readvFile(fd int, iovs []IOVec) (int, error) {
|
||||
if len(iovs) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
n, _, errno := syscall.Syscall(syscall.SYS_READV,
|
||||
uintptr(fd),
|
||||
uintptr(unsafe.Pointer(&iovs[0])),
|
||||
uintptr(len(iovs)))
|
||||
|
||||
if errno != 0 {
|
||||
return 0, errno
|
||||
}
|
||||
return int(n), nil
|
||||
}
|
||||
|
||||
func writevFile(fd int, iovs []IOVec) (int, error) {
|
||||
if len(iovs) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
n, _, errno := syscall.Syscall(syscall.SYS_WRITEV,
|
||||
uintptr(fd),
|
||||
uintptr(unsafe.Pointer(&iovs[0])),
|
||||
uintptr(len(iovs)))
|
||||
|
||||
if errno != 0 {
|
||||
return 0, errno
|
||||
}
|
||||
return int(n), nil
|
||||
}
|
||||
|
||||
func preadvFile(fd int, iovs []IOVec, offset int64) (int, error) {
|
||||
if len(iovs) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// preadv/pwritev may not be available on all Unix systems
|
||||
// Fall back to individual pread calls
|
||||
totalRead := 0
|
||||
currentOffset := offset
|
||||
|
||||
for _, iov := range iovs {
|
||||
if iov.Len == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
buf := (*[1 << 30]byte)(unsafe.Pointer(iov.Base))[:iov.Len:iov.Len]
|
||||
n, err := syscall.Pread(fd, buf, currentOffset)
|
||||
if err != nil {
|
||||
return totalRead, err
|
||||
}
|
||||
totalRead += n
|
||||
currentOffset += int64(n)
|
||||
|
||||
if n < int(iov.Len) {
|
||||
break // EOF or partial read
|
||||
}
|
||||
}
|
||||
|
||||
return totalRead, nil
|
||||
}
|
||||
|
||||
func pwritevFile(fd int, iovs []IOVec, offset int64) (int, error) {
|
||||
if len(iovs) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// preadv/pwritev may not be available on all Unix systems
|
||||
// Fall back to individual pwrite calls
|
||||
totalWritten := 0
|
||||
currentOffset := offset
|
||||
|
||||
for _, iov := range iovs {
|
||||
if iov.Len == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
buf := (*[1 << 30]byte)(unsafe.Pointer(iov.Base))[:iov.Len:iov.Len]
|
||||
n, err := syscall.Pwrite(fd, buf, currentOffset)
|
||||
if err != nil {
|
||||
return totalWritten, err
|
||||
}
|
||||
totalWritten += n
|
||||
currentOffset += int64(n)
|
||||
|
||||
if n < int(iov.Len) {
|
||||
break // Partial write
|
||||
}
|
||||
}
|
||||
|
||||
return totalWritten, nil
|
||||
}
|
||||
|
||||
// Helper function to create IOVec from byte slices
|
||||
func makeIOVecs(buffers [][]byte) []IOVec {
|
||||
iovs := make([]IOVec, len(buffers))
|
||||
for i, buf := range buffers {
|
||||
if len(buf) > 0 {
|
||||
iovs[i].Base = &buf[0]
|
||||
iovs[i].Len = uint64(len(buf))
|
||||
}
|
||||
}
|
||||
return iovs
|
||||
}
|
||||
|
||||
func isVectoredIOSupported() bool {
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
//go:build !unix
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Vectored I/O support for unsupported platforms
|
||||
|
||||
var ErrVectoredIONotSupported = errors.New("vectored I/O not supported on this platform")
|
||||
|
||||
// IOVec represents an I/O vector for readv/writev operations
|
||||
type IOVec struct {
|
||||
Base *byte
|
||||
Len uint64
|
||||
}
|
||||
|
||||
func readvFile(fd int, iovs []IOVec) (int, error) {
|
||||
return 0, ErrVectoredIONotSupported
|
||||
}
|
||||
|
||||
func writevFile(fd int, iovs []IOVec) (int, error) {
|
||||
return 0, ErrVectoredIONotSupported
|
||||
}
|
||||
|
||||
func preadvFile(fd int, iovs []IOVec, offset int64) (int, error) {
|
||||
return 0, ErrVectoredIONotSupported
|
||||
}
|
||||
|
||||
func pwritevFile(fd int, iovs []IOVec, offset int64) (int, error) {
|
||||
return 0, ErrVectoredIONotSupported
|
||||
}
|
||||
|
||||
func makeIOVecs(buffers [][]byte) []IOVec {
|
||||
return nil
|
||||
}
|
||||
|
||||
func isVectoredIOSupported() bool {
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
//go:build darwin
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Extended attributes support for macOS
|
||||
|
||||
const (
|
||||
// macOS-specific flags
|
||||
XATTR_NOFOLLOW = 0x0001
|
||||
XATTR_CREATE = 0x0002
|
||||
XATTR_REPLACE = 0x0004
|
||||
)
|
||||
|
||||
func setXattr(path, name string, value []byte, flags int) error {
|
||||
pathBytes, err := syscall.BytePtrFromString(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nameBytes, err := syscall.BytePtrFromString(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var valuePtr unsafe.Pointer
|
||||
if len(value) > 0 {
|
||||
valuePtr = unsafe.Pointer(&value[0])
|
||||
}
|
||||
|
||||
_, _, errno := syscall.Syscall6(syscall.SYS_SETXATTR,
|
||||
uintptr(unsafe.Pointer(pathBytes)),
|
||||
uintptr(unsafe.Pointer(nameBytes)),
|
||||
uintptr(valuePtr),
|
||||
uintptr(len(value)),
|
||||
uintptr(0), // position (not used for regular files)
|
||||
uintptr(flags))
|
||||
|
||||
if errno != 0 {
|
||||
return errno
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getXattr(path, name string, value []byte) (int, error) {
|
||||
pathBytes, err := syscall.BytePtrFromString(path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
nameBytes, err := syscall.BytePtrFromString(name)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var valuePtr unsafe.Pointer
|
||||
if len(value) > 0 {
|
||||
valuePtr = unsafe.Pointer(&value[0])
|
||||
}
|
||||
|
||||
size, _, errno := syscall.Syscall6(syscall.SYS_GETXATTR,
|
||||
uintptr(unsafe.Pointer(pathBytes)),
|
||||
uintptr(unsafe.Pointer(nameBytes)),
|
||||
uintptr(valuePtr),
|
||||
uintptr(len(value)),
|
||||
uintptr(0), // position
|
||||
uintptr(0)) // options
|
||||
|
||||
if errno != 0 {
|
||||
return 0, errno
|
||||
}
|
||||
return int(size), nil
|
||||
}
|
||||
|
||||
func listXattr(path string, list []byte) (int, error) {
|
||||
pathBytes, err := syscall.BytePtrFromString(path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var listPtr unsafe.Pointer
|
||||
if len(list) > 0 {
|
||||
listPtr = unsafe.Pointer(&list[0])
|
||||
}
|
||||
|
||||
size, _, errno := syscall.Syscall6(syscall.SYS_LISTXATTR,
|
||||
uintptr(unsafe.Pointer(pathBytes)),
|
||||
uintptr(listPtr),
|
||||
uintptr(len(list)),
|
||||
uintptr(0), // options
|
||||
0, 0)
|
||||
|
||||
if errno != 0 {
|
||||
return 0, errno
|
||||
}
|
||||
return int(size), nil
|
||||
}
|
||||
|
||||
func removeXattr(path, name string) error {
|
||||
pathBytes, err := syscall.BytePtrFromString(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nameBytes, err := syscall.BytePtrFromString(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, _, errno := syscall.Syscall6(syscall.SYS_REMOVEXATTR,
|
||||
uintptr(unsafe.Pointer(pathBytes)),
|
||||
uintptr(unsafe.Pointer(nameBytes)),
|
||||
uintptr(0), // options
|
||||
0, 0, 0)
|
||||
|
||||
if errno != 0 {
|
||||
return errno
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isXattrSupported() bool {
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
//go:build linux
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Extended attributes support for Linux
|
||||
|
||||
func setXattr(path, name string, value []byte, flags int) error {
|
||||
pathBytes, err := syscall.BytePtrFromString(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nameBytes, err := syscall.BytePtrFromString(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var valuePtr unsafe.Pointer
|
||||
if len(value) > 0 {
|
||||
valuePtr = unsafe.Pointer(&value[0])
|
||||
}
|
||||
|
||||
_, _, errno := syscall.Syscall6(syscall.SYS_SETXATTR,
|
||||
uintptr(unsafe.Pointer(pathBytes)),
|
||||
uintptr(unsafe.Pointer(nameBytes)),
|
||||
uintptr(valuePtr),
|
||||
uintptr(len(value)),
|
||||
uintptr(flags),
|
||||
0)
|
||||
|
||||
if errno != 0 {
|
||||
return errno
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getXattr(path, name string, value []byte) (int, error) {
|
||||
pathBytes, err := syscall.BytePtrFromString(path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
nameBytes, err := syscall.BytePtrFromString(name)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var valuePtr unsafe.Pointer
|
||||
if len(value) > 0 {
|
||||
valuePtr = unsafe.Pointer(&value[0])
|
||||
}
|
||||
|
||||
size, _, errno := syscall.Syscall6(syscall.SYS_GETXATTR,
|
||||
uintptr(unsafe.Pointer(pathBytes)),
|
||||
uintptr(unsafe.Pointer(nameBytes)),
|
||||
uintptr(valuePtr),
|
||||
uintptr(len(value)),
|
||||
0,
|
||||
0)
|
||||
|
||||
if errno != 0 {
|
||||
return 0, errno
|
||||
}
|
||||
return int(size), nil
|
||||
}
|
||||
|
||||
func listXattr(path string, list []byte) (int, error) {
|
||||
pathBytes, err := syscall.BytePtrFromString(path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var listPtr unsafe.Pointer
|
||||
if len(list) > 0 {
|
||||
listPtr = unsafe.Pointer(&list[0])
|
||||
}
|
||||
|
||||
size, _, errno := syscall.Syscall(syscall.SYS_LISTXATTR,
|
||||
uintptr(unsafe.Pointer(pathBytes)),
|
||||
uintptr(listPtr),
|
||||
uintptr(len(list)))
|
||||
|
||||
if errno != 0 {
|
||||
return 0, errno
|
||||
}
|
||||
return int(size), nil
|
||||
}
|
||||
|
||||
func removeXattr(path, name string) error {
|
||||
pathBytes, err := syscall.BytePtrFromString(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nameBytes, err := syscall.BytePtrFromString(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, _, errno := syscall.Syscall(syscall.SYS_REMOVEXATTR,
|
||||
uintptr(unsafe.Pointer(pathBytes)),
|
||||
uintptr(unsafe.Pointer(nameBytes)),
|
||||
0)
|
||||
|
||||
if errno != 0 {
|
||||
return errno
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isXattrSupported() bool {
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
//go:build !linux && !darwin
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Extended attributes support for unsupported platforms
|
||||
|
||||
var ErrXattrNotSupported = errors.New("extended attributes not supported on this platform")
|
||||
|
||||
func setXattr(path, name string, value []byte, flags int) error {
|
||||
return ErrXattrNotSupported
|
||||
}
|
||||
|
||||
func getXattr(path, name string, value []byte) (int, error) {
|
||||
return 0, ErrXattrNotSupported
|
||||
}
|
||||
|
||||
func listXattr(path string, list []byte) (int, error) {
|
||||
return 0, ErrXattrNotSupported
|
||||
}
|
||||
|
||||
func removeXattr(path, name string) error {
|
||||
return ErrXattrNotSupported
|
||||
}
|
||||
|
||||
func isXattrSupported() bool {
|
||||
return false
|
||||
}
|
||||
@@ -24,7 +24,7 @@ require (
|
||||
github.com/jcmturner/gofork v1.7.6 // indirect
|
||||
github.com/jcmturner/gokrb5/v8 v8.4.4 // indirect
|
||||
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
|
||||
github.com/klauspost/compress v1.18.0 // indirect
|
||||
github.com/klauspost/compress v1.18.7 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.22 // indirect
|
||||
|
||||
@@ -38,8 +38,8 @@ github.com/jcmturner/gokrb5/v8 v8.4.4 h1:x1Sv4HaTpepFkXbt2IkL29DXRf8sOfZXo8eRKh6
|
||||
github.com/jcmturner/gokrb5/v8 v8.4.4/go.mod h1:1btQEpgT6k+unzCwX1KdWMEwPPkkgBtP+F6aCACiMrs=
|
||||
github.com/jcmturner/rpc/v2 v2.0.3 h1:7FXXj8Ti1IaVFpSAziCZWNzbNuZmnvw/i6CqLNdWfZY=
|
||||
github.com/jcmturner/rpc/v2 v2.0.3/go.mod h1:VUJYCIDm3PVOEHw8sgt091/20OJjskO/YJki3ELg/Hc=
|
||||
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
|
||||
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
|
||||
github.com/klauspost/compress v1.18.7 h1:aUyZsS4kH3QTKurYhAOwAHxllVPnOthb3vPfnF1Ehjw=
|
||||
github.com/klauspost/compress v1.18.7/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
|
||||
@@ -5,6 +5,6 @@ go 1.24.7
|
||||
require github.com/segmentio/kafka-go v0.4.49
|
||||
|
||||
require (
|
||||
github.com/klauspost/compress v1.15.9 // indirect
|
||||
github.com/klauspost/compress v1.18.7 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.15 // indirect
|
||||
)
|
||||
@@ -1,7 +1,7 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
|
||||
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
|
||||
github.com/klauspost/compress v1.18.7 h1:aUyZsS4kH3QTKurYhAOwAHxllVPnOthb3vPfnF1Ehjw=
|
||||
github.com/klauspost/compress v1.18.7/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
|
||||
github.com/pierrec/lz4/v4 v4.1.15 h1:MO0/ucJhngq7299dKLwIMtgTfbkoSPF6AoMYDd8Q4q0=
|
||||
github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
module simple-consumer
|
||||
|
||||
go 1.21
|
||||
go 1.24
|
||||
|
||||
require github.com/segmentio/kafka-go v0.4.47
|
||||
|
||||
require (
|
||||
github.com/klauspost/compress v1.17.0 // indirect
|
||||
github.com/klauspost/compress v1.18.7 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.15 // indirect
|
||||
)
|
||||
@@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
|
||||
github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
|
||||
github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
|
||||
github.com/klauspost/compress v1.18.7 h1:aUyZsS4kH3QTKurYhAOwAHxllVPnOthb3vPfnF1Ehjw=
|
||||
github.com/klauspost/compress v1.18.7/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
|
||||
github.com/pierrec/lz4/v4 v4.1.15 h1:MO0/ucJhngq7299dKLwIMtgTfbkoSPF6AoMYDd8Q4q0=
|
||||
github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
module simple-publisher
|
||||
|
||||
go 1.21
|
||||
go 1.25
|
||||
|
||||
require github.com/segmentio/kafka-go v0.4.47
|
||||
|
||||
require (
|
||||
github.com/klauspost/compress v1.17.0 // indirect
|
||||
github.com/klauspost/compress v1.20.3 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.15 // indirect
|
||||
)
|
||||
@@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
|
||||
github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
|
||||
github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
|
||||
github.com/klauspost/compress v1.20.3 h1:zWNoFqq/lSuYQRim3dUXlXeA0IqwuZGicdG5ZTfY5j4=
|
||||
github.com/klauspost/compress v1.20.3/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI=
|
||||
github.com/pierrec/lz4/v4 v4.1.15 h1:MO0/ucJhngq7299dKLwIMtgTfbkoSPF6AoMYDd8Q4q0=
|
||||
github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
package s3api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// Check both wire APIs with small pages. The directory walker alone does
|
||||
// not exercise prefix normalization, delimiter-marker adjustment, or tokens.
|
||||
func TestListingPrefixAndDelimiterPagination(t *testing.T) {
|
||||
client := getS3Client(t)
|
||||
ctx := context.Background()
|
||||
for _, versioning := range []string{"", "Enabled", "Suspended"} {
|
||||
t.Run("versioning="+versioning, func(t *testing.T) {
|
||||
bucket := getNewBucketName()
|
||||
createBucket(t, client, bucket)
|
||||
defer deleteBucket(t, client, bucket)
|
||||
if versioning != "" {
|
||||
enableVersioning(t, client, bucket)
|
||||
if versioning == "Suspended" {
|
||||
_, err := client.PutBucketVersioning(ctx, &s3.PutBucketVersioningInput{
|
||||
Bucket: aws.String(bucket), VersioningConfiguration: &types.VersioningConfiguration{Status: types.BucketVersioningStatusSuspended},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
}
|
||||
for _, key := range []string{"a", "a-c", "a.txt", "a/b", "foo_bar", "foo_bar_baz", "foo_baz"} {
|
||||
_, err := client.PutObject(ctx, &s3.PutObjectInput{Bucket: aws.String(bucket), Key: aws.String(key), Body: bytes.NewReader([]byte("x"))})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name, prefix, delimiter, startAfter string
|
||||
keys, prefixes []string
|
||||
}{
|
||||
{"trailing slash", "a/", "", "", []string{"a/b"}, nil},
|
||||
{"trailing slash custom delimiter", "a/", "_", "", []string{"a/b"}, nil},
|
||||
{"custom delimiter", "foo", "_", "", nil, []string{"foo_"}},
|
||||
{"custom delimiter nested group", "foo_", "_", "", []string{"foo_bar", "foo_baz"}, []string{"foo_bar_"}},
|
||||
{"marker names directory", "a", "", "a", []string{"a-c", "a.txt", "a/b"}, nil},
|
||||
{"marker between name and slash", "a", "", "a.txt", []string{"a/b"}, nil},
|
||||
} {
|
||||
for _, v2 := range []bool{false, true} {
|
||||
for _, pageSize := range []int32{1, 2, 1000} {
|
||||
t.Run(fmt.Sprintf("%s/v2=%t/max=%d", tc.name, v2, pageSize), func(t *testing.T) {
|
||||
var keys, prefixes []string
|
||||
marker, token := tc.startAfter, ""
|
||||
for page := 0; ; page++ {
|
||||
require.Less(t, page, 10, "listing must terminate without repeating a prefix")
|
||||
var objects []types.Object
|
||||
var commonPrefixes []types.CommonPrefix
|
||||
var truncated bool
|
||||
if v2 {
|
||||
in := &s3.ListObjectsV2Input{Bucket: aws.String(bucket), Prefix: aws.String(tc.prefix), Delimiter: aws.String(tc.delimiter), MaxKeys: aws.Int32(pageSize)}
|
||||
if token != "" {
|
||||
in.ContinuationToken = aws.String(token)
|
||||
} else if marker != "" {
|
||||
in.StartAfter = aws.String(marker)
|
||||
}
|
||||
out, err := client.ListObjectsV2(ctx, in)
|
||||
require.NoError(t, err)
|
||||
objects, commonPrefixes, truncated = out.Contents, out.CommonPrefixes, aws.ToBool(out.IsTruncated)
|
||||
next := aws.ToString(out.NextContinuationToken)
|
||||
if truncated {
|
||||
require.NotEmpty(t, next)
|
||||
require.NotEqual(t, token, next, "token must advance")
|
||||
}
|
||||
token = next
|
||||
} else {
|
||||
out, err := client.ListObjects(ctx, &s3.ListObjectsInput{
|
||||
Bucket: aws.String(bucket), Prefix: aws.String(tc.prefix), Delimiter: aws.String(tc.delimiter), MaxKeys: aws.Int32(pageSize), Marker: aws.String(marker),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
objects, commonPrefixes, truncated = out.Contents, out.CommonPrefixes, aws.ToBool(out.IsTruncated)
|
||||
next := aws.ToString(out.NextMarker)
|
||||
if truncated {
|
||||
if next == "" && len(objects) > 0 {
|
||||
next = aws.ToString(objects[len(objects)-1].Key)
|
||||
}
|
||||
require.Greater(t, next, marker, "marker must advance")
|
||||
}
|
||||
marker = next
|
||||
}
|
||||
for _, object := range objects {
|
||||
keys = append(keys, aws.ToString(object.Key))
|
||||
}
|
||||
for _, prefix := range commonPrefixes {
|
||||
prefixes = append(prefixes, aws.ToString(prefix.Prefix))
|
||||
}
|
||||
if !truncated {
|
||||
break
|
||||
}
|
||||
}
|
||||
sort.Strings(keys)
|
||||
sort.Strings(prefixes)
|
||||
assert.Equal(t, tc.keys, keys)
|
||||
assert.Equal(t, tc.prefixes, prefixes)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package s3tables
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/smithy-go"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func newObjectClient(t *testing.T) *s3.Client {
|
||||
t.Helper()
|
||||
cfg, err := config.LoadDefaultConfig(context.Background(),
|
||||
config.WithRegion(testRegion),
|
||||
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(testAccessKey, testSecretKey, "")),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
return s3.NewFromConfig(cfg, func(o *s3.Options) {
|
||||
o.BaseEndpoint = aws.String(sharedCluster.s3Endpoint)
|
||||
o.UsePathStyle = true
|
||||
})
|
||||
}
|
||||
|
||||
func apiErrorCode(err error) string {
|
||||
var apiErr smithy.APIError
|
||||
if errors.As(err, &apiErr) {
|
||||
return apiErr.ErrorCode()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// The external-volume verification object round-trips at a table bucket's
|
||||
// root the way the check uses it: PUT, HEAD, GET, list, DELETE. Any other key
|
||||
// at the root is still refused.
|
||||
func TestTableBucketExternalVolumeVerificationRoundTrip(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping integration test in short mode")
|
||||
}
|
||||
ctx := context.Background()
|
||||
tables := NewS3TablesClient(sharedCluster.s3Endpoint, testRegion, testAccessKey, testSecretKey)
|
||||
bucket := "verify-" + randomString(8)
|
||||
created, err := tables.CreateTableBucket(bucket, nil)
|
||||
require.NoError(t, err)
|
||||
defer tables.DeleteTableBucket(created.ARN)
|
||||
client := newObjectClient(t)
|
||||
|
||||
body := []byte("verification payload!") // 21 bytes
|
||||
key := fmt.Sprintf("verify_%d_%d", time.Now().UnixMilli(), 1081846483)
|
||||
|
||||
// the control: an ordinary key at the root is refused
|
||||
_, err = client.PutObject(ctx, &s3.PutObjectInput{Bucket: aws.String(bucket), Key: aws.String("notes.parquet"), Body: bytes.NewReader(body)})
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, "AccessDenied", apiErrorCode(err))
|
||||
|
||||
_, err = client.PutObject(ctx, &s3.PutObjectInput{Bucket: aws.String(bucket), Key: aws.String(key), Body: bytes.NewReader(body), ContentLength: aws.Int64(int64(len(body)))})
|
||||
require.NoError(t, err)
|
||||
|
||||
head, err := client.HeadObject(ctx, &s3.HeadObjectInput{Bucket: aws.String(bucket), Key: aws.String(key)})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(len(body)), aws.ToInt64(head.ContentLength))
|
||||
|
||||
got, err := client.GetObject(ctx, &s3.GetObjectInput{Bucket: aws.String(bucket), Key: aws.String(key)})
|
||||
require.NoError(t, err)
|
||||
data, err := io.ReadAll(got.Body)
|
||||
got.Body.Close()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, body, data)
|
||||
|
||||
list, err := client.ListObjectsV2(ctx, &s3.ListObjectsV2Input{Bucket: aws.String(bucket), Prefix: aws.String("verify_")})
|
||||
require.NoError(t, err)
|
||||
var keys []string
|
||||
for _, o := range list.Contents {
|
||||
keys = append(keys, aws.ToString(o.Key))
|
||||
}
|
||||
assert.Equal(t, []string{key}, keys)
|
||||
|
||||
_, err = client.DeleteObject(ctx, &s3.DeleteObjectInput{Bucket: aws.String(bucket), Key: aws.String(key)})
|
||||
require.NoError(t, err)
|
||||
_, err = client.HeadObject(ctx, &s3.HeadObjectInput{Bucket: aws.String(bucket), Key: aws.String(key)})
|
||||
require.Error(t, err, "the verification object must be gone after DELETE")
|
||||
}
|
||||
|
||||
// A namespace can carry a name of the verification key's shape. The key
|
||||
// then names the namespace's directory: a PUT or DELETE through it must not
|
||||
// replace or remove the namespace.
|
||||
func TestTableBucketVerificationKeyLeavesNamespaceAlone(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping integration test in short mode")
|
||||
}
|
||||
ctx := context.Background()
|
||||
tables := NewS3TablesClient(sharedCluster.s3Endpoint, testRegion, testAccessKey, testSecretKey)
|
||||
bucket := "verify-ns-" + randomString(8)
|
||||
created, err := tables.CreateTableBucket(bucket, nil)
|
||||
require.NoError(t, err)
|
||||
defer tables.DeleteTableBucket(created.ARN)
|
||||
client := newObjectClient(t)
|
||||
body := []byte("verification payload!")
|
||||
|
||||
listed := func(name string) bool {
|
||||
resp, err := tables.ListNamespaces(created.ARN, "", "", 0)
|
||||
require.NoError(t, err)
|
||||
for _, ns := range resp.Namespaces {
|
||||
if len(ns.Namespace) == 1 && ns.Namespace[0] == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
for i, op := range []string{"PUT", "DELETE"} {
|
||||
t.Run(op, func(t *testing.T) {
|
||||
name := fmt.Sprintf("verify_%d_%d", time.Now().UnixMilli(), 7+i)
|
||||
_, err := tables.CreateNamespace(created.ARN, []string{name})
|
||||
require.NoError(t, err)
|
||||
defer tables.DeleteNamespace(created.ARN, []string{name})
|
||||
require.True(t, listed(name), "setup: the namespace is listed")
|
||||
|
||||
if op == "PUT" {
|
||||
_, err = client.PutObject(ctx, &s3.PutObjectInput{Bucket: aws.String(bucket), Key: aws.String(name), Body: bytes.NewReader(body), ContentLength: aws.Int64(int64(len(body)))})
|
||||
assert.Error(t, err, "a PUT over a namespace must be refused")
|
||||
} else {
|
||||
_, err = client.DeleteObject(ctx, &s3.DeleteObjectInput{Bucket: aws.String(bucket), Key: aws.String(name)})
|
||||
assert.Error(t, err, "a DELETE of a namespace must be refused")
|
||||
}
|
||||
_, err = tables.GetNamespace(created.ARN, []string{name})
|
||||
assert.NoError(t, err, "the namespace survives the %s", op)
|
||||
assert.True(t, listed(name), "the namespace is still listed after the %s", op)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,15 @@ var (
|
||||
// S3 gateway, whose remote-mount read paths dial the mounted endpoint directly.
|
||||
const allowUntrustedRemoteEndpointsUsage = "if true, a read of a remote-only entry accepts arbitrary remote S3 endpoints including loopback / link-local hosts. Default rejects internal / metadata endpoints."
|
||||
|
||||
// iamSelfServiceRequiresGrantUsage documents the flag shared by every command
|
||||
// that runs the embedded IAM API.
|
||||
const iamSelfServiceRequiresGrantUsage = "if true, an identity needs an explicit iam: grant to manage its own access keys (CreateAccessKey, DeleteAccessKey, UpdateAccessKey, ListAccessKeys, GetUser), as on AWS. Default allows it to every authenticated identity."
|
||||
|
||||
// allowBackslashInKeyUsage documents the S3 flag keeping '\' a literal
|
||||
// object-key character instead of normalizing it to '/'. Repeated '//' in a
|
||||
// key still collapses: a filer entry name cannot contain an empty segment.
|
||||
const allowBackslashInKeyUsage = "treat backslash as a regular object-key character instead of converting it to '/' (aws-compatible; '//' still collapses)"
|
||||
|
||||
type FilerOptions struct {
|
||||
masters *pb.ServerDiscovery
|
||||
mastersString *string
|
||||
@@ -173,6 +182,8 @@ func init() {
|
||||
filerS3Options.cacheSizeMB = cmdFiler.Flag.Int64("s3.cacheCapacityMB", 0, "in-memory chunk cache capacity in MB for S3 GETs shared across requests (0 disables)")
|
||||
filerS3Options.readerCacheSizeMB = cmdFiler.Flag.Int64("s3.readerCacheSizeMB", 0, "memory budget in MiB for downloaded and in-flight reader buffers across all S3 GETs (0 means unlimited)")
|
||||
filerS3Options.allowUntrustedRemoteEndpoints = cmdFiler.Flag.Bool("s3.allowUntrustedRemoteEndpoints", false, allowUntrustedRemoteEndpointsUsage)
|
||||
filerS3Options.allowBackslashInKey = cmdFiler.Flag.Bool("s3.allowBackslashInKey", false, allowBackslashInKeyUsage)
|
||||
filerS3Options.iamSelfServiceRequiresGrant = cmdFiler.Flag.Bool("s3.iam.selfServiceRequiresGrant", false, iamSelfServiceRequiresGrantUsage)
|
||||
|
||||
// start webdav on filer
|
||||
filerStartWebDav = cmdFiler.Flag.Bool("webdav", false, "whether to start webdav gateway")
|
||||
@@ -191,6 +202,7 @@ func init() {
|
||||
filerStartIam = cmdFiler.Flag.Bool("iam", false, "whether to start IAM service")
|
||||
filerIamOptions.ip = cmdFiler.Flag.String("iam.ip", *f.ip, "iam server http listen ip address")
|
||||
filerIamOptions.port = cmdFiler.Flag.Int("iam.port", 8111, "iam server http listen port")
|
||||
filerIamOptions.selfServiceRequiresGrant = filerS3Options.iamSelfServiceRequiresGrant
|
||||
|
||||
filerStartSftp = cmdFiler.Flag.Bool("sftp", false, "whether to start the SFTP server")
|
||||
filerSftpOptions.port = cmdFiler.Flag.Int("sftp.port", 2022, "SFTP server listen port")
|
||||
|
||||
@@ -384,6 +384,16 @@ func processUpdateEvent(
|
||||
}
|
||||
glog.V(0).Infof("never replicated, uploading %s", remote_storage.FormatLocation(dest))
|
||||
}
|
||||
if proto.Equal(oldDest, dest) && isDroppedLocalCopy(message.NewEntry) {
|
||||
// The update dropped the local copy: remote.uncache, eviction under
|
||||
// storage pressure, or remote.meta.sync pulling a newer remote
|
||||
// version. Each clears the sync stamp, so shouldSendToRemote reads
|
||||
// the entry as dirty when the remote mtime shares its second. The
|
||||
// content is the object at this very key, and uploading the chunkless
|
||||
// entry would replace it with zero bytes.
|
||||
glog.V(2).Infof("skipping update of remote-only entry: %+v", resp)
|
||||
return nil
|
||||
}
|
||||
glog.V(2).Infof("update: %+v", resp)
|
||||
if !proto.Equal(oldDest, dest) {
|
||||
// A renamed entry that holds no local data now (the snapshot was
|
||||
@@ -488,6 +498,15 @@ func isRemoteOnly(entry *filer_pb.Entry) bool {
|
||||
return entry != nil && !filer.HasData(entry) && entry.IsInRemoteOnly()
|
||||
}
|
||||
|
||||
// isDroppedLocalCopy reports a remote-only entry whose size is still the
|
||||
// remote object's: its local copy was dropped and the object describes it.
|
||||
// Uncache and eviction keep the size, and remote.meta.sync sets it from the
|
||||
// object. A truncate drops the chunks too but changes the size, and the
|
||||
// truncated content has to reach the remote.
|
||||
func isDroppedLocalCopy(entry *filer_pb.Entry) bool {
|
||||
return isRemoteOnly(entry) && entry.GetAttributes().GetFileSize() == uint64(entry.RemoteEntry.RemoteSize)
|
||||
}
|
||||
|
||||
// completeRemoteOnlyRename finishes a rename whose content exists only on the
|
||||
// remote. When the destination object already holds what the entry's stamp
|
||||
// describes (the rename ran before, its offset was not persisted, and
|
||||
|
||||
@@ -1722,6 +1722,206 @@ func TestMetadataOnlyUpdateRestampsSyncTs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The droppedCopy fixtures are a small synced file under a mount: a Delta log
|
||||
// entry whose remote object was written in the second of its mtime, as a quick
|
||||
// sync of a small file is.
|
||||
const droppedCopyMtime = 1786096669
|
||||
|
||||
var (
|
||||
droppedCopyDir = "/buckets/gold/sales/_delta_log"
|
||||
droppedCopyName = "00000000000000000007.json"
|
||||
droppedCopyMount = &remote_pb.RemoteStorageLocation{Name: "s3", Bucket: "gold", Path: "/"}
|
||||
droppedCopyDest = &remote_pb.RemoteStorageLocation{Name: "s3", Bucket: "gold", Path: "/sales/_delta_log/" + droppedCopyName}
|
||||
droppedCopySynced = &filer_pb.RemoteEntry{StorageName: "s3", RemoteETag: `"abc"`, RemoteSize: 1024, RemoteMtime: droppedCopyMtime, LastLocalSyncTsNs: droppedCopyMtime*1e9 + 500}
|
||||
)
|
||||
|
||||
// droppedCopyStamp is the synced stamp with LastLocalSyncTsNs cleared, as
|
||||
// remote.uncache and the eviction leave it.
|
||||
func droppedCopyStamp() *filer_pb.RemoteEntry {
|
||||
stamp := proto.Clone(droppedCopySynced).(*filer_pb.RemoteEntry)
|
||||
stamp.LastLocalSyncTsNs = 0
|
||||
return stamp
|
||||
}
|
||||
|
||||
// droppedCopyEntry is the fixture file with the given stamp and chunks, sized
|
||||
// as its remote object.
|
||||
func droppedCopyEntry(stamp *filer_pb.RemoteEntry, chunks ...*filer_pb.FileChunk) *filer_pb.Entry {
|
||||
entry := entryWith(droppedCopyName, stamp, chunks...)
|
||||
entry.Attributes.FileSize = uint64(droppedCopySynced.RemoteSize)
|
||||
return entry
|
||||
}
|
||||
|
||||
func droppedCopyUpdate(oldEntry, newEntry *filer_pb.Entry) *filer_pb.SubscribeMetadataResponse {
|
||||
return &filer_pb.SubscribeMetadataResponse{
|
||||
Directory: droppedCopyDir,
|
||||
EventNotification: &filer_pb.EventNotification{OldEntry: oldEntry, NewParentPath: droppedCopyDir, NewEntry: newEntry},
|
||||
}
|
||||
}
|
||||
|
||||
// TestDroppedLocalCopyIsNotUploaded: remote.uncache and the filer's eviction
|
||||
// under storage pressure drop a synced entry's chunks and clear its sync
|
||||
// stamp; remote.meta.sync does the same when it pulls a newer remote version.
|
||||
// With the remote mtime in the entry's second, shouldSendToRemote falls back
|
||||
// to the cleared stamp and reads the entry as dirty. The object at the same
|
||||
// key is the entry's only content, so uploading the chunkless entry would
|
||||
// replace it with a zero-byte object. The update is skipped, and the stamp
|
||||
// stays cleared so remote.cache and eviction still see the entry as uncached.
|
||||
func TestDroppedLocalCopyIsNotUploaded(t *testing.T) {
|
||||
// the eviction's entry, built the way uncacheRemoteEntry builds it, and
|
||||
// the event the way the filer's CreateEntry emits it
|
||||
cached := &filer.Entry{
|
||||
FullPath: util.NewFullPath(droppedCopyDir, droppedCopyName),
|
||||
Attr: filer.Attr{Mtime: time.Unix(droppedCopyMtime, 0), Mode: 0660, FileSize: 1024},
|
||||
Chunks: []*filer_pb.FileChunk{chunk("3,01", "e1")},
|
||||
Remote: droppedCopySynced,
|
||||
}
|
||||
evicted := cached.ShallowClone()
|
||||
evicted.Chunks = nil
|
||||
evicted.Remote = proto.Clone(cached.Remote).(*filer_pb.RemoteEntry)
|
||||
evicted.Remote.LastLocalSyncTsNs = 0
|
||||
|
||||
// remote.meta.sync found a newer object: its mtime becomes the entry's
|
||||
newer := &filer_pb.RemoteEntry{StorageName: "s3", RemoteETag: `"def"`, RemoteSize: 4096, RemoteMtime: droppedCopyMtime + 60}
|
||||
pulled := &filer_pb.Entry{Name: droppedCopyName, Attributes: &filer_pb.FuseAttributes{Mtime: droppedCopyMtime + 60, FileSize: 4096}, RemoteEntry: newer}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
oldEntry *filer_pb.Entry
|
||||
newEntry *filer_pb.Entry
|
||||
}{
|
||||
{name: "remote.uncache", oldEntry: droppedCopyEntry(droppedCopySynced, chunk("3,01", "e1")), newEntry: droppedCopyEntry(droppedCopyStamp())},
|
||||
{name: "eviction under storage pressure", oldEntry: cached.ToProtoEntry(), newEntry: evicted.ToProtoEntry()},
|
||||
{name: "remote.meta.sync pulls a newer version", oldEntry: entryWith(droppedCopyName, droppedCopySynced, chunk("3,01", "e1")), newEntry: pulled},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
resp := droppedCopyUpdate(tt.oldEntry, tt.newEntry)
|
||||
if !shouldSendToRemote(tt.newEntry) {
|
||||
t.Fatal("precondition: the cleared stamp in the remote mtime's second reads as dirty, the bug's trigger")
|
||||
}
|
||||
if isMetadataOnlyUpdate(resp.Directory, resp.EventNotification) {
|
||||
t.Fatal("precondition: the dropped chunks make the update a content change")
|
||||
}
|
||||
|
||||
remote := &recordingRemote{}
|
||||
filerClient := &stubFilerClient{entry: tt.newEntry}
|
||||
if err := processUpdateEvent(filerClient, filerClient, "", remote, "/buckets/gold", droppedCopyMount, resp); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(remote.writes) != 0 {
|
||||
t.Errorf("writes = %+v, want none: the remote object is the entry's content and an upload would zero it", remote.writes)
|
||||
}
|
||||
if len(remote.deletes) != 0 || len(remote.metadataUpdates) != 0 {
|
||||
t.Errorf("deletes = %+v, metadata updates = %+v, want the remote object left alone", remote.deletes, remote.metadataUpdates)
|
||||
}
|
||||
if filerClient.updates != 0 {
|
||||
t.Errorf("stamped the entry %d times, want 0: a stamp would mark the uncached entry as cached", filerClient.updates)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("a metadata edit of a remote-only entry still reaches the remote", func(t *testing.T) {
|
||||
tagged := droppedCopyEntry(droppedCopyStamp())
|
||||
tagged.Extended = map[string][]byte{"X-Amz-Tagging-tier": []byte("gold")}
|
||||
resp := droppedCopyUpdate(droppedCopyEntry(droppedCopyStamp()), tagged)
|
||||
remote := &recordingRemote{}
|
||||
filerClient := &stubFilerClient{entry: tagged}
|
||||
if err := processUpdateEvent(filerClient, filerClient, "", remote, "/buckets/gold", droppedCopyMount, resp); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(remote.metadataUpdates) != 1 || len(remote.writes) != 0 {
|
||||
t.Errorf("metadata updates = %+v, writes = %+v, want one metadata update and no upload", remote.metadataUpdates, remote.writes)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestSameSecondRewriteIsStillUploaded: a rewrite that lands in the remote
|
||||
// mtime's second holds local data, so the skip for dropped local copies does
|
||||
// not apply and it is uploaded and restamped (#11715).
|
||||
func TestSameSecondRewriteIsStillUploaded(t *testing.T) {
|
||||
stale := proto.Clone(droppedCopySynced).(*filer_pb.RemoteEntry)
|
||||
stale.LastLocalSyncTsNs = droppedCopyMtime*1e9 + 100
|
||||
rewrite := func(stamp *filer_pb.RemoteEntry) *filer_pb.Entry {
|
||||
entry := entryWith(droppedCopyName, stamp, chunk("4,02", "e2"))
|
||||
entry.Attributes.MtimeNs = 337
|
||||
return entry
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
oldEntry *filer_pb.Entry
|
||||
newEntry *filer_pb.Entry
|
||||
}{
|
||||
// Filer.UpdateEntry clears the stamp of a content change
|
||||
{name: "synced entry, stamp cleared by the filer", oldEntry: entryWith(droppedCopyName, droppedCopySynced, chunk("3,01", "e1")), newEntry: rewrite(droppedCopyStamp())},
|
||||
{name: "synced entry, stamp older than the rewrite", oldEntry: entryWith(droppedCopyName, stale, chunk("3,01", "e1")), newEntry: rewrite(stale)},
|
||||
{name: "uncached entry", oldEntry: entryWith(droppedCopyName, droppedCopyStamp()), newEntry: rewrite(droppedCopyStamp())},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.newEntry.Attributes.Mtime != tt.newEntry.RemoteEntry.RemoteMtime {
|
||||
t.Fatal("precondition: the rewrite lands in the remote mtime's second")
|
||||
}
|
||||
remote := &recordingRemote{}
|
||||
filerClient := &stubFilerClient{entry: tt.newEntry}
|
||||
if err := processUpdateEvent(filerClient, filerClient, "", remote, "/buckets/gold", droppedCopyMount, droppedCopyUpdate(tt.oldEntry, tt.newEntry)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(remote.writes) != 1 || !proto.Equal(remote.writes[0], droppedCopyDest) {
|
||||
t.Fatalf("writes = %+v, want the rewrite uploaded to %s", remote.writes, remote_storage.FormatLocation(droppedCopyDest))
|
||||
}
|
||||
if !slices.Equal(remote.writtenChunks[0], []string{"4,02"}) {
|
||||
t.Errorf("uploaded chunks %v, want the rewrite's [4,02]", remote.writtenChunks[0])
|
||||
}
|
||||
if filerClient.updates != 1 {
|
||||
t.Errorf("stamped the entry %d times, want 1 after the upload", filerClient.updates)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestTruncateToZeroIsUploaded: a FUSE truncate of a cached file to zero
|
||||
// (truncateEntry, or SetAttr with size 0) drops the chunks and keeps the
|
||||
// sized RemoteEntry, so the entry looks remote-only. Its size is no longer
|
||||
// the remote object's, and the empty file has to replace the object.
|
||||
func TestTruncateToZeroIsUploaded(t *testing.T) {
|
||||
truncated := func(mtime int64, mtimeNs int32) *filer_pb.Entry {
|
||||
// the filer clears the stamp of a content change
|
||||
entry := entryWith(droppedCopyName, droppedCopyStamp())
|
||||
entry.Attributes.Mtime, entry.Attributes.MtimeNs = mtime, mtimeNs
|
||||
return entry
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
newEntry *filer_pb.Entry
|
||||
}{
|
||||
{name: "in a later second", newEntry: truncated(droppedCopyMtime+5, 0)},
|
||||
{name: "in the remote mtime's second", newEntry: truncated(droppedCopyMtime, 337)},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if !isRemoteOnly(tt.newEntry) {
|
||||
t.Fatal("precondition: the truncated entry looks remote-only")
|
||||
}
|
||||
remote := &recordingRemote{}
|
||||
filerClient := &stubFilerClient{entry: tt.newEntry}
|
||||
resp := droppedCopyUpdate(droppedCopyEntry(droppedCopySynced, chunk("3,01", "e1")), tt.newEntry)
|
||||
if err := processUpdateEvent(filerClient, filerClient, "", remote, "/buckets/gold", droppedCopyMount, resp); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(remote.writes) != 1 || !proto.Equal(remote.writes[0], droppedCopyDest) {
|
||||
t.Fatalf("writes = %+v, want the empty file uploaded to %s", remote.writes, remote_storage.FormatLocation(droppedCopyDest))
|
||||
}
|
||||
if len(remote.writtenChunks[0]) != 0 {
|
||||
t.Errorf("uploaded chunks %v, want none: a zero-byte object", remote.writtenChunks[0])
|
||||
}
|
||||
if filerClient.updates != 1 {
|
||||
t.Errorf("stamped the entry %d times, want 1 after the upload", filerClient.updates)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompleteRemoteOnlyRenameKeepsAReusedOldKey(t *testing.T) {
|
||||
// A was renamed to B, then D to A before the sync got to the first
|
||||
// rename: the object at A is now D's, and the only copy of it
|
||||
|
||||
+11
-8
@@ -28,10 +28,11 @@ var (
|
||||
)
|
||||
|
||||
type IamOptions struct {
|
||||
filer *string
|
||||
masters *string
|
||||
ip *string
|
||||
port *int
|
||||
filer *string
|
||||
masters *string
|
||||
ip *string
|
||||
port *int
|
||||
selfServiceRequiresGrant *bool
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -40,6 +41,7 @@ func init() {
|
||||
iamStandaloneOptions.masters = cmdIam.Flag.String("master", "localhost:9333", "comma-separated master servers")
|
||||
iamStandaloneOptions.ip = cmdIam.Flag.String("ip", util.DetectedHostAddress(), "iam server http listen ip address")
|
||||
iamStandaloneOptions.port = cmdIam.Flag.Int("port", 8111, "iam server http listen port")
|
||||
iamStandaloneOptions.selfServiceRequiresGrant = cmdIam.Flag.Bool("selfServiceRequiresGrant", false, iamSelfServiceRequiresGrantUsage)
|
||||
}
|
||||
|
||||
var cmdIam = &Command{
|
||||
@@ -101,10 +103,11 @@ func (iamopt *IamOptions) startIamServer() bool {
|
||||
masters := pb.ServerAddresses(*iamopt.masters).ToAddressMap()
|
||||
router := mux.NewRouter().SkipClean(true)
|
||||
iamApiServer, iamApiServer_err := iamapi.NewIamApiServer(router, &iamapi.IamServerOption{
|
||||
Masters: masters,
|
||||
Filers: filerAddresses,
|
||||
Port: *iamopt.port,
|
||||
GrpcDialOption: grpcDialOption,
|
||||
Masters: masters,
|
||||
Filers: filerAddresses,
|
||||
Port: *iamopt.port,
|
||||
GrpcDialOption: grpcDialOption,
|
||||
IamSelfServiceRequiresGrant: *iamopt.selfServiceRequiresGrant,
|
||||
})
|
||||
glog.V(0).Info("NewIamApiServer created")
|
||||
if iamApiServer_err != nil {
|
||||
|
||||
@@ -556,6 +556,8 @@ func initMiniS3Flags() {
|
||||
miniS3Options.cacheSizeMB = cmdMini.Flag.Int64("s3.cacheCapacityMB", 0, "in-memory chunk cache capacity in MB for S3 GETs shared across requests (0 disables)")
|
||||
miniS3Options.readerCacheSizeMB = cmdMini.Flag.Int64("s3.readerCacheSizeMB", 0, "memory budget in MiB for downloaded and in-flight reader buffers across all S3 GETs (0 means unlimited)")
|
||||
miniS3Options.allowUntrustedRemoteEndpoints = cmdMini.Flag.Bool("s3.allowUntrustedRemoteEndpoints", false, allowUntrustedRemoteEndpointsUsage)
|
||||
miniS3Options.allowBackslashInKey = cmdMini.Flag.Bool("s3.allowBackslashInKey", false, allowBackslashInKeyUsage)
|
||||
miniS3Options.iamSelfServiceRequiresGrant = cmdMini.Flag.Bool("s3.iam.selfServiceRequiresGrant", false, iamSelfServiceRequiresGrantUsage)
|
||||
// In mini mode, S3 uses the shared debug server started at line 681, not its own separate debug server
|
||||
miniS3Options.debug = new(bool) // explicitly false
|
||||
miniS3Options.debugPort = cmdMini.Flag.Int("s3.debug.port", 6060, "http port for debugging (unused in mini mode)")
|
||||
|
||||
+8
-1
@@ -84,6 +84,8 @@ type S3Options struct {
|
||||
readerCacheSizeMB *int64
|
||||
|
||||
allowUntrustedRemoteEndpoints *bool
|
||||
allowBackslashInKey *bool
|
||||
iamSelfServiceRequiresGrant *bool
|
||||
// shutdownCtx, when non-nil, tells startS3Server to gracefully shut down
|
||||
// its HTTP/gRPC servers once the ctx is cancelled, in addition to on
|
||||
// interrupt. Used by weed mini to orchestrate an ordered shutdown.
|
||||
@@ -140,6 +142,8 @@ func init() {
|
||||
s3StandaloneOptions.cacheSizeMB = cmdS3.Flag.Int64("cacheCapacityMB", 0, "in-memory chunk cache capacity in MB for S3 GETs shared across requests (0 disables)")
|
||||
s3StandaloneOptions.readerCacheSizeMB = cmdS3.Flag.Int64("readerCacheSizeMB", 0, "memory budget in MiB for downloaded and in-flight reader buffers across all S3 GETs (0 means unlimited)")
|
||||
s3StandaloneOptions.allowUntrustedRemoteEndpoints = cmdS3.Flag.Bool("allowUntrustedRemoteEndpoints", false, allowUntrustedRemoteEndpointsUsage)
|
||||
s3StandaloneOptions.allowBackslashInKey = cmdS3.Flag.Bool("allowBackslashInKey", false, allowBackslashInKeyUsage)
|
||||
s3StandaloneOptions.iamSelfServiceRequiresGrant = cmdS3.Flag.Bool("iam.selfServiceRequiresGrant", false, iamSelfServiceRequiresGrantUsage)
|
||||
}
|
||||
|
||||
var cmdS3 = &Command{
|
||||
@@ -337,7 +341,8 @@ func (s3opt *S3Options) startS3Server() bool {
|
||||
|
||||
router := mux.NewRouter().SkipClean(true)
|
||||
var localFilerSocket string
|
||||
if s3opt.localFilerSocket != nil {
|
||||
// The filer only listens on its local socket outside Windows.
|
||||
if s3opt.localFilerSocket != nil && runtime.GOOS != "windows" {
|
||||
localFilerSocket = *s3opt.localFilerSocket
|
||||
}
|
||||
var s3ApiServer *s3api.S3ApiServer
|
||||
@@ -399,6 +404,8 @@ func (s3opt *S3Options) startS3Server() bool {
|
||||
MaxMB: filerMaxMB,
|
||||
|
||||
AllowUntrustedRemoteEndpoints: *s3opt.allowUntrustedRemoteEndpoints,
|
||||
AllowBackslashInKey: *s3opt.allowBackslashInKey,
|
||||
IamSelfServiceRequiresGrant: *s3opt.iamSelfServiceRequiresGrant,
|
||||
})
|
||||
if s3ApiServer_err != nil {
|
||||
glog.Fatalf("S3 API Server startup error: %v", s3ApiServer_err)
|
||||
|
||||
@@ -33,12 +33,22 @@ recursive_delete = false
|
||||
# faster than previous leveldb, recommended.
|
||||
enabled = true
|
||||
dir = "./filerldb2" # directory to store level db files
|
||||
# optional tuning, applied to each of the 8 partitioned stores
|
||||
# block_cache_size_mb = 32 # sorted-table read cache
|
||||
# write_buffer_size_mb = 16 # memtable size before flushing to disk
|
||||
# block_size_kb = 4 # uncompressed block ('page') size inside table files
|
||||
# compaction_table_size_mb = 2 # target size of generated table files
|
||||
|
||||
[leveldb3]
|
||||
# similar to leveldb2.
|
||||
# each bucket has its own meta store.
|
||||
enabled = false
|
||||
dir = "./filerldb3" # directory to store level db files
|
||||
# optional tuning, applied to the default store and each bucket store
|
||||
# block_cache_size_mb = 32 # sorted-table read cache
|
||||
# write_buffer_size_mb = 16 # memtable size before flushing to disk
|
||||
# block_size_kb = 4 # uncompressed block ('page') size inside table files
|
||||
# compaction_table_size_mb = 2 # target size of generated table files
|
||||
|
||||
[rocksdb]
|
||||
# local on disk, similar to leveldb
|
||||
|
||||
@@ -28,6 +28,10 @@ region = "us-east-2"
|
||||
bucket = "your_bucket_name" # an existing bucket
|
||||
endpoint = ""
|
||||
storage_class = "STANDARD_IA"
|
||||
# optional HTTP client tuning for S3-compatible services:
|
||||
# disable_http2 = false # true keeps the client on HTTP/1.1, one connection per concurrent request
|
||||
# max_connections = 0 # cap on connections to the endpoint, 0 = no cap
|
||||
# idle_connection_timeout_seconds = 0 # close idle connections sooner, 0 = Go default (90s)
|
||||
|
||||
# create this number of logical volumes if no more writable volumes
|
||||
# count_x means how many copies of data.
|
||||
|
||||
@@ -195,10 +195,12 @@ func init() {
|
||||
s3Options.iamReadOnly = cmdServer.Flag.Bool("s3.iam.readOnly", true, "disable IAM write operations on this server")
|
||||
s3Options.cipher = cmdServer.Flag.Bool("s3.encryptVolumeData", false, "encrypt data on volume servers for S3 uploads")
|
||||
s3Options.externalUrl = cmdServer.Flag.String("s3.externalUrl", "", "the external URL clients use to connect (e.g. https://api.example.com:9000). Advertised to Iceberg and Lance clients, and tried first when verifying S3 signatures behind a reverse proxy. Falls back to S3_EXTERNAL_URL env var.")
|
||||
s3Options.allowBackslashInKey = cmdServer.Flag.Bool("s3.allowBackslashInKey", false, allowBackslashInKeyUsage)
|
||||
s3Options.defaultFileMode = cmdServer.Flag.String("s3.defaultFileMode", "", "default file mode for S3 uploaded objects, e.g. 0660, 0644, 0666")
|
||||
s3Options.cacheSizeMB = cmdServer.Flag.Int64("s3.cacheCapacityMB", 0, "in-memory chunk cache capacity in MB for S3 GETs shared across requests (0 disables)")
|
||||
s3Options.readerCacheSizeMB = cmdServer.Flag.Int64("s3.readerCacheSizeMB", 0, "memory budget in MiB for downloaded and in-flight reader buffers across all S3 GETs (0 means unlimited)")
|
||||
s3Options.allowUntrustedRemoteEndpoints = cmdServer.Flag.Bool("s3.allowUntrustedRemoteEndpoints", false, allowUntrustedRemoteEndpointsUsage)
|
||||
s3Options.iamSelfServiceRequiresGrant = cmdServer.Flag.Bool("s3.iam.selfServiceRequiresGrant", false, iamSelfServiceRequiresGrantUsage)
|
||||
|
||||
sftpOptions.port = cmdServer.Flag.Int("sftp.port", 2022, "SFTP server listen port")
|
||||
sftpOptions.sshPrivateKey = cmdServer.Flag.String("sftp.sshPrivateKey", "", "path to the SSH private key file for host authentication")
|
||||
@@ -214,6 +216,7 @@ func init() {
|
||||
sftpOptions.dataCenter = cmdServer.Flag.String("sftp.dataCenter", "", "prefer to read and write to volumes in this data center")
|
||||
sftpOptions.localSocket = cmdServer.Flag.String("sftp.localSocket", "", "default to /tmp/seaweedfs-sftp-<port>.sock")
|
||||
iamOptions.port = cmdServer.Flag.Int("iam.port", 8111, "iam server http listen port")
|
||||
iamOptions.selfServiceRequiresGrant = s3Options.iamSelfServiceRequiresGrant
|
||||
|
||||
webdavOptions.port = cmdServer.Flag.Int("webdav.port", 7333, "webdav server http listen port")
|
||||
webdavOptions.collection = cmdServer.Flag.String("webdav.collection", "", "collection to create the files")
|
||||
|
||||
@@ -3,6 +3,7 @@ package command
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
|
||||
@@ -12,10 +13,11 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
shellOptions shell.ShellOptions
|
||||
shellInitialFiler *string
|
||||
shellCluster *string
|
||||
shellDebug *bool
|
||||
shellOptions shell.ShellOptions
|
||||
shellInitialFiler *string
|
||||
shellCluster *string
|
||||
shellDebug *bool
|
||||
shellConnectTimeout *time.Duration
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -25,6 +27,7 @@ func init() {
|
||||
shellInitialFiler = cmdShell.Flag.String("filer", "", "filer host and port for initial connection, e.g. localhost:8888")
|
||||
shellCluster = cmdShell.Flag.String("cluster", "", "cluster defined in shell.toml")
|
||||
shellDebug = cmdShell.Flag.Bool("debug", false, "print informational logs to stderr")
|
||||
shellConnectTimeout = cmdShell.Flag.Duration("connectTimeout", 30*time.Second, "timeout for the initial master connection and filer discovery")
|
||||
}
|
||||
|
||||
var cmdShell = &Command{
|
||||
@@ -64,6 +67,7 @@ func runShell(command *Command, args []string) bool {
|
||||
}
|
||||
shellOptions.FilerAddress = pb.ServerAddress(filerAddress)
|
||||
shellOptions.Debug = *shellDebug
|
||||
shellOptions.ConnectTimeout = *shellConnectTimeout
|
||||
if shellOptions.Debug {
|
||||
fmt.Fprintf(os.Stderr, "master: %s filer: %s\n", *shellOptions.Masters, shellOptions.FilerAddress)
|
||||
}
|
||||
|
||||
+73
-19
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
||||
@@ -31,6 +32,7 @@ type UploadOptions struct {
|
||||
diskType *string
|
||||
maxMB *int
|
||||
usePublicUrl *bool
|
||||
concurrency *int
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -46,6 +48,7 @@ func init() {
|
||||
upload.ttl = cmdUpload.Flag.String("ttl", "", "time to live, e.g.: 1m, 1h, 1d, 1M, 1y")
|
||||
upload.maxMB = cmdUpload.Flag.Int("maxMB", 4, "split files larger than the limit")
|
||||
upload.usePublicUrl = cmdUpload.Flag.Bool("usePublicUrl", false, "upload to public url from volume server")
|
||||
upload.concurrency = cmdUpload.Flag.Int("c", 8, "concurrent file upload goroutines when uploading a folder")
|
||||
}
|
||||
|
||||
var cmdUpload = &Command{
|
||||
@@ -85,6 +88,7 @@ func runUpload(cmd *Command, args []string) bool {
|
||||
if *upload.dir == "" {
|
||||
return false
|
||||
}
|
||||
var filePaths []string
|
||||
err = filepath.Walk(util.ResolvePath(*upload.dir), func(path string, info os.FileInfo, err error) error {
|
||||
if err == nil {
|
||||
if !info.IsDir() {
|
||||
@@ -93,23 +97,7 @@ func runUpload(cmd *Command, args []string) bool {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
parts, e := operation.NewFileParts([]string{path})
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
results, e := operation.SubmitFiles(func(_ context.Context) pb.ServerAddress { return pb.ServerAddress(*upload.master) }, grpcDialOption, parts, operation.StoragePreference{
|
||||
Replication: *upload.replication,
|
||||
Collection: *upload.collection,
|
||||
DataCenter: *upload.dataCenter,
|
||||
Ttl: *upload.ttl,
|
||||
DiskType: *upload.diskType,
|
||||
MaxMB: *upload.maxMB,
|
||||
}, *upload.usePublicUrl)
|
||||
bytes, _ := json.Marshal(results)
|
||||
fmt.Println(string(bytes))
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
filePaths = append(filePaths, path)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
@@ -118,6 +106,72 @@ func runUpload(cmd *Command, args []string) bool {
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
concurrency := *upload.concurrency
|
||||
if concurrency < 1 {
|
||||
concurrency = 1
|
||||
}
|
||||
masterFn := func(_ context.Context) pb.ServerAddress { return pb.ServerAddress(*upload.master) }
|
||||
pref := operation.StoragePreference{
|
||||
Replication: *upload.replication,
|
||||
Collection: *upload.collection,
|
||||
DataCenter: *upload.dataCenter,
|
||||
Ttl: *upload.ttl,
|
||||
DiskType: *upload.diskType,
|
||||
MaxMB: *upload.maxMB,
|
||||
}
|
||||
results := make([][]operation.SubmitResult, len(filePaths))
|
||||
ready := make([]chan struct{}, len(filePaths))
|
||||
for index := range ready {
|
||||
ready[index] = make(chan struct{})
|
||||
}
|
||||
printDone := make(chan bool, 1)
|
||||
go func() {
|
||||
failed := false
|
||||
for index := range ready {
|
||||
<-ready[index]
|
||||
bytes, _ := json.Marshal(results[index])
|
||||
fmt.Println(string(bytes))
|
||||
for _, r := range results[index] {
|
||||
if r.Error != "" {
|
||||
failed = true
|
||||
}
|
||||
}
|
||||
}
|
||||
printDone <- failed
|
||||
}()
|
||||
|
||||
sem := make(chan struct{}, concurrency)
|
||||
var wg sync.WaitGroup
|
||||
for index, filePath := range filePaths {
|
||||
sem <- struct{}{}
|
||||
wg.Add(1)
|
||||
go func(index int, filePath string) {
|
||||
defer close(ready[index])
|
||||
defer wg.Done()
|
||||
defer func() { <-sem }()
|
||||
parts, e := operation.NewFileParts([]string{filePath})
|
||||
if e != nil {
|
||||
results[index] = []operation.SubmitResult{{FileName: filepath.Base(filePath), Error: e.Error()}}
|
||||
return
|
||||
}
|
||||
fileResults, e := operation.SubmitFiles(masterFn, grpcDialOption, parts, pref, *upload.usePublicUrl)
|
||||
if e != nil {
|
||||
for i := range fileResults {
|
||||
if fileResults[i].Error == "" {
|
||||
fileResults[i].Error = e.Error()
|
||||
}
|
||||
}
|
||||
}
|
||||
results[index] = fileResults
|
||||
}(index, filePath)
|
||||
}
|
||||
wg.Wait()
|
||||
if <-printDone {
|
||||
return false
|
||||
}
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
@@ -134,12 +188,12 @@ func runUpload(cmd *Command, args []string) bool {
|
||||
DiskType: *upload.diskType,
|
||||
MaxMB: *upload.maxMB,
|
||||
}, *upload.usePublicUrl)
|
||||
bytes, _ := json.Marshal(results)
|
||||
fmt.Println(string(bytes))
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return false
|
||||
}
|
||||
bytes, _ := json.Marshal(results)
|
||||
fmt.Println(string(bytes))
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -120,13 +120,20 @@ func (store *IamGrpcStore) CreateAccessKey(ctx context.Context, username string,
|
||||
}
|
||||
|
||||
func (store *IamGrpcStore) DeleteAccessKey(ctx context.Context, username string, accessKey string) error {
|
||||
return store.withIamClient(ctx, func(ctx context.Context, client iam_pb.SeaweedIdentityAccessManagementClient) error {
|
||||
err := store.withIamClient(ctx, func(ctx context.Context, client iam_pb.SeaweedIdentityAccessManagementClient) error {
|
||||
_, err := client.DeleteAccessKey(ctx, &iam_pb.DeleteAccessKeyRequest{
|
||||
Username: username,
|
||||
AccessKey: accessKey,
|
||||
})
|
||||
return err
|
||||
})
|
||||
// The filer-side handler returns gRPC NotFound when the user or the key is
|
||||
// absent; translate back to the sentinel so callers can use
|
||||
// errors.Is(err, credential.ErrAccessKeyNotFound) uniformly across stores.
|
||||
if err != nil && status.Code(err) == codes.NotFound {
|
||||
return credential.ErrAccessKeyNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// AttachUserPolicy attaches a managed policy to a user by policy name
|
||||
|
||||
@@ -2,6 +2,7 @@ package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
@@ -106,3 +107,50 @@ func TestIamGrpcStore_AdminBearerToken(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestIamGrpcStore_DeleteAccessKeyNotFound pins the not-found translation:
|
||||
// the filer reports a missing user or key as gRPC NotFound, and the store must
|
||||
// surface it as credential.ErrAccessKeyNotFound so repeat deletes stay a no-op
|
||||
// for IAM dispatchers that tolerate the sentinel.
|
||||
func TestIamGrpcStore_DeleteAccessKeyNotFound(t *testing.T) {
|
||||
const goodKey = "iam-admin-itest-key"
|
||||
|
||||
cm, err := credential.NewCredentialManager(credential.StoreTypeMemory, nil, "")
|
||||
if err != nil {
|
||||
t.Fatalf("NewCredentialManager: %v", err)
|
||||
}
|
||||
defer cm.Shutdown()
|
||||
|
||||
iamSrv := weed_server.NewIamGrpcServer(cm, security.SigningKey(goodKey))
|
||||
|
||||
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("listen: %v", err)
|
||||
}
|
||||
grpcSrv := grpc.NewServer()
|
||||
iam_pb.RegisterSeaweedIdentityAccessManagementServer(grpcSrv, iamSrv)
|
||||
go func() { _ = grpcSrv.Serve(lis) }()
|
||||
t.Cleanup(grpcSrv.Stop)
|
||||
|
||||
_, portStr, err := net.SplitHostPort(lis.Addr().String())
|
||||
if err != nil {
|
||||
t.Fatalf("split listener addr: %v", err)
|
||||
}
|
||||
serverAddr := pb.ServerAddress(fmt.Sprintf("127.0.0.1:0.%s", portStr))
|
||||
|
||||
store := &IamGrpcStore{}
|
||||
store.SetFilerAddressFunc(func() pb.ServerAddress { return serverAddr }, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
store.SetAdminSigning(security.SigningKey(goodKey), 0)
|
||||
|
||||
ctx := context.Background()
|
||||
if err := store.CreateUser(ctx, &iam_pb.Identity{Name: "alice"}); err != nil {
|
||||
t.Fatalf("CreateUser: %v", err)
|
||||
}
|
||||
|
||||
if err := store.DeleteAccessKey(ctx, "alice", "AKIAMISSING0000000001"); !errors.Is(err, credential.ErrAccessKeyNotFound) {
|
||||
t.Fatalf("DeleteAccessKey with missing key: got %v, want ErrAccessKeyNotFound", err)
|
||||
}
|
||||
if err := store.DeleteAccessKey(ctx, "ghost", "AKIAMISSING0000000001"); !errors.Is(err, credential.ErrAccessKeyNotFound) {
|
||||
t.Fatalf("DeleteAccessKey with missing user: got %v, want ErrAccessKeyNotFound", err)
|
||||
}
|
||||
}
|
||||
@@ -142,6 +142,14 @@ func (store *MemoryStore) UpdateUser(ctx context.Context, username string, ident
|
||||
return credential.ErrUserNotFound
|
||||
}
|
||||
|
||||
if identity.Name != username {
|
||||
// Rename: the destination name must not already be taken, matching
|
||||
// RenameUser's collision check.
|
||||
if _, clash := store.users[identity.Name]; clash {
|
||||
return credential.ErrUserAlreadyExists
|
||||
}
|
||||
}
|
||||
|
||||
// Remove old access keys from index
|
||||
for _, cred := range existingUser.Credentials {
|
||||
delete(store.accessKeys, cred.AccessKey)
|
||||
@@ -156,11 +164,15 @@ func (store *MemoryStore) UpdateUser(ctx context.Context, username string, ident
|
||||
|
||||
// Deep copy to avoid mutation issues
|
||||
identityCopy := store.deepCopyIdentity(identity)
|
||||
store.users[username] = identityCopy
|
||||
if identityCopy.Name != username {
|
||||
// Rename: re-key the record, the way filer_etc does (delete old, save new).
|
||||
delete(store.users, username)
|
||||
}
|
||||
store.users[identityCopy.Name] = identityCopy
|
||||
|
||||
// Re-index access keys
|
||||
for _, cred := range identity.Credentials {
|
||||
store.accessKeys[cred.AccessKey] = username
|
||||
store.accessKeys[cred.AccessKey] = identityCopy.Name
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -378,3 +378,82 @@ func TestMemoryStoreServiceAccountByAccessKey(t *testing.T) {
|
||||
t.Errorf("Expected ErrAccessKeyNotFound after delete, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMemoryStoreUpdateUserRename: UpdateUser with an identity whose Name
|
||||
// differs from the key must re-key the record, the way filer_etc does
|
||||
// (delete old, save new). The IAM rename path relies on this.
|
||||
func TestMemoryStoreUpdateUserRename(t *testing.T) {
|
||||
store := &MemoryStore{}
|
||||
if err := store.Initialize(util.GetViper(), "credential."); err != nil {
|
||||
t.Fatalf("Failed to initialize store: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
err := store.CreateUser(ctx, &iam_pb.Identity{
|
||||
Name: "oldname",
|
||||
Credentials: []*iam_pb.Credential{{AccessKey: "access123", SecretKey: "secret123"}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create user: %v", err)
|
||||
}
|
||||
|
||||
err = store.UpdateUser(ctx, "oldname", &iam_pb.Identity{
|
||||
Name: "newname",
|
||||
Credentials: []*iam_pb.Credential{{AccessKey: "access123", SecretKey: "secret123"}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to rename user via UpdateUser: %v", err)
|
||||
}
|
||||
|
||||
if _, err := store.GetUser(ctx, "oldname"); err != credential.ErrUserNotFound {
|
||||
t.Fatalf("Expected ErrUserNotFound for old name, got %v", err)
|
||||
}
|
||||
renamed, err := store.GetUser(ctx, "newname")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get renamed user: %v", err)
|
||||
}
|
||||
if renamed.Name != "newname" {
|
||||
t.Errorf("Expected 'newname', got '%s'", renamed.Name)
|
||||
}
|
||||
byKey, err := store.GetUserByAccessKey(ctx, "access123")
|
||||
if err != nil {
|
||||
t.Fatalf("Access key must resolve to the renamed user: %v", err)
|
||||
}
|
||||
if byKey.Name != "newname" {
|
||||
t.Errorf("Expected access key owner 'newname', got '%s'", byKey.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMemoryStoreUpdateUserRenameCollision: a rename whose destination name is
|
||||
// taken must fail rather than overwrite the existing user.
|
||||
func TestMemoryStoreUpdateUserRenameCollision(t *testing.T) {
|
||||
store := &MemoryStore{}
|
||||
if err := store.Initialize(util.GetViper(), "credential."); err != nil {
|
||||
t.Fatalf("Failed to initialize store: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
if err := store.CreateUser(ctx, &iam_pb.Identity{Name: "alice"}); err != nil {
|
||||
t.Fatalf("Failed to create alice: %v", err)
|
||||
}
|
||||
if err := store.CreateUser(ctx, &iam_pb.Identity{
|
||||
Name: "bob",
|
||||
Credentials: []*iam_pb.Credential{{AccessKey: "bobkey123", SecretKey: "secret123"}},
|
||||
}); err != nil {
|
||||
t.Fatalf("Failed to create bob: %v", err)
|
||||
}
|
||||
|
||||
err := store.UpdateUser(ctx, "alice", &iam_pb.Identity{Name: "bob"})
|
||||
if err != credential.ErrUserAlreadyExists {
|
||||
t.Fatalf("Expected ErrUserAlreadyExists, got %v", err)
|
||||
}
|
||||
|
||||
// Both users must be intact.
|
||||
if _, err := store.GetUser(ctx, "alice"); err != nil {
|
||||
t.Fatalf("alice must still exist: %v", err)
|
||||
}
|
||||
bob, err := store.GetUserByAccessKey(ctx, "bobkey123")
|
||||
if err != nil || bob.Name != "bob" {
|
||||
t.Fatalf("bob's access key must still resolve to bob: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -23,14 +23,20 @@ type SqlGenerator interface {
|
||||
GetSqlDeleteFolderChildren(tableName string) string
|
||||
GetSqlListExclusive(tableName string) string
|
||||
GetSqlListInclusive(tableName string) string
|
||||
// GetSqlScanPage returns the query and arguments selecting dirhash, name,
|
||||
// directory and meta of up to limit rows after the (dirhash, name) cursor.
|
||||
GetSqlScanPage(tableName string, dirhash int64, name string, limit int) (string, []interface{})
|
||||
GetSqlCreateTable(tableName string) string
|
||||
GetSqlDropTable(tableName string) string
|
||||
}
|
||||
|
||||
type AbstractSqlStore struct {
|
||||
SqlGenerator
|
||||
DB *sql.DB
|
||||
KvDB *sql.DB
|
||||
DB *sql.DB
|
||||
KvDB *sql.DB
|
||||
// ScanDB, when set, is the pool snapshot scans run on (SQLite's main pool
|
||||
// is a single connection that a long scan must not hold).
|
||||
ScanDB *sql.DB
|
||||
SupportBucketTable bool
|
||||
SkipDDL bool
|
||||
dbs map[string]bool
|
||||
@@ -457,6 +463,9 @@ func (store *AbstractSqlStore) Shutdown() {
|
||||
if store.KvDB != nil {
|
||||
store.KvDB.Close()
|
||||
}
|
||||
if store.ScanDB != nil {
|
||||
store.ScanDB.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func isValidBucket(bucket string) bool {
|
||||
|
||||
@@ -0,0 +1,368 @@
|
||||
package abstract_sql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
)
|
||||
|
||||
var _ filer.EntryScanner = (*AbstractSqlStore)(nil)
|
||||
|
||||
// scanTable is one table to read during a scan together with the path its
|
||||
// directory column is relative to: "" for the default table, whose rows hold
|
||||
// full directories, and "/buckets/<bucket>" for a per-bucket table.
|
||||
type scanTable struct {
|
||||
table string
|
||||
base string
|
||||
// cachedOnly marks a table that only a stale bucket cache remembers: its
|
||||
// bucket directory was already gone from the snapshot, so a missing table
|
||||
// means it was dropped meanwhile, not that rows are absent.
|
||||
cachedOnly bool
|
||||
}
|
||||
|
||||
// scanPageSize is how many rows one keyset page of a scan fetches.
|
||||
var scanPageSize = 1000
|
||||
|
||||
// SetScanPageSizeForTest lets tests drive the keyset cursor across pages. It
|
||||
// returns the previous size.
|
||||
func SetScanPageSizeForTest(n int) int {
|
||||
prev := scanPageSize
|
||||
scanPageSize = n
|
||||
return prev
|
||||
}
|
||||
|
||||
// ScanEntries implements filer.EntryScanner: every table that can hold an
|
||||
// entry below dirPath is read inside one REPEATABLE READ, read-only
|
||||
// transaction, in primary-key pages. All rows are seen as of one instant; a
|
||||
// rename committed meanwhile shows up under exactly one of its names. The
|
||||
// transaction takes no row locks and does not block writers; it does hold the
|
||||
// tables it has read against DDL, so a bucket deleted while the scan runs
|
||||
// drops its table once the scan ends. A page's cursor is closed before its
|
||||
// entries go to fn, so fn may read the store through the context it receives
|
||||
// (which carries the transaction) on the same connection.
|
||||
func (store *AbstractSqlStore) ScanEntries(ctx context.Context, dirPath util.FullPath, fn func(ctx context.Context, entry *filer.Entry) error) error {
|
||||
// Read before the transaction holds a connection: OnBucketCreation keeps
|
||||
// dbsLock while it waits for a connection of its own, so taking the lock
|
||||
// while holding a connection could deadlock on an exhausted pool.
|
||||
cachedBuckets := store.cachedBucketNames()
|
||||
|
||||
tx, err := store.beginSnapshotTx(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Read-only: nothing to commit, and a rollback releases the snapshot.
|
||||
defer tx.Rollback()
|
||||
|
||||
tables, err := store.scanTables(ctx, tx, dirPath, cachedBuckets)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Reads made through this context (getTxOrDB, kvDB) use the transaction
|
||||
// and therefore the snapshot.
|
||||
txCtx := context.WithValue(ctx, "tx", tx)
|
||||
// The root's own row may live in a table the scan does not read (a
|
||||
// bucket directory is in the default table, its contents in the bucket's).
|
||||
if root, err := store.readRowInSnapshot(txCtx, tx, dirPath); err != nil {
|
||||
return err
|
||||
} else if root != nil {
|
||||
if err := fn(txCtx, root); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, t := range tables {
|
||||
if err := store.scanOneTable(txCtx, tx, t, dirPath, fn); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// readRowInSnapshot reads one entry's row inside the transaction, routing to
|
||||
// the table getTxOrDB would use but without its bucket-table creation. A
|
||||
// missing row is nil.
|
||||
func (store *AbstractSqlStore) readRowInSnapshot(ctx context.Context, tx *sql.Tx, p util.FullPath) (*filer.Entry, error) {
|
||||
if p == "/" {
|
||||
return nil, nil
|
||||
}
|
||||
parent, name := p.DirAndName()
|
||||
table, shortDir := DEFAULT_TABLE, parent
|
||||
if store.SupportBucketTable {
|
||||
if bucket := bucketTableFor(util.FullPath(parent)); bucket != "" {
|
||||
table = bucket
|
||||
if shortDir = strings.TrimPrefix(parent, bucketBase(bucket)); shortDir == "" {
|
||||
shortDir = "/"
|
||||
}
|
||||
}
|
||||
}
|
||||
var data []byte
|
||||
err := tx.QueryRowContext(ctx, store.GetSqlFind(table), util.HashStringToLong(shortDir), name, shortDir).Scan(&data)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, missingTableOr(err, table, fmt.Sprintf("read %s", p))
|
||||
}
|
||||
entry := &filer.Entry{FullPath: p}
|
||||
if err := entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data)); err != nil {
|
||||
return nil, fmt.Errorf("decode %s: %v", p, err)
|
||||
}
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
// beginSnapshotTx starts the read-only REPEATABLE READ transaction that gives
|
||||
// one snapshot across statements. Nothing weaker is tried: under READ
|
||||
// COMMITTED each page would see a different snapshot, and the stream would
|
||||
// still be advertised as one.
|
||||
func (store *AbstractSqlStore) beginSnapshotTx(ctx context.Context) (*sql.Tx, error) {
|
||||
db := store.DB
|
||||
if store.ScanDB != nil {
|
||||
db = store.ScanDB
|
||||
} else if db.Stats().MaxOpenConnections == 1 {
|
||||
// The scan would hold the filer's only connection for its whole run.
|
||||
return nil, fmt.Errorf("%w: the store's single connection cannot be held for a scan", filer.ErrUnsupportedEntryScan)
|
||||
}
|
||||
// A BeginTx failure (a down database) must fail the traversal, not route it
|
||||
// to the walk: a recovered database lets the walk read renamed-away entries
|
||||
// as absent, the very loss this scan exists to prevent.
|
||||
tx, err := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelRepeatableRead, ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("begin snapshot: %v", err)
|
||||
}
|
||||
return tx, nil
|
||||
}
|
||||
|
||||
func (store *AbstractSqlStore) cachedBucketNames() []string {
|
||||
store.dbsLock.Lock()
|
||||
defer store.dbsLock.Unlock()
|
||||
names := make([]string, 0, len(store.dbs))
|
||||
for name := range store.dbs {
|
||||
names = append(names, name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// scanTables picks the tables to read for dirPath: the default table alone,
|
||||
// one bucket's table for a path inside that bucket, or the default table plus
|
||||
// every bucket table (enumerated from /buckets inside the snapshot).
|
||||
func (store *AbstractSqlStore) scanTables(ctx context.Context, tx *sql.Tx, dirPath util.FullPath, cachedBuckets []string) ([]scanTable, error) {
|
||||
if !store.SupportBucketTable {
|
||||
return []scanTable{{table: DEFAULT_TABLE}}, nil
|
||||
}
|
||||
if bucket := bucketTableFor(dirPath); bucket != "" {
|
||||
return []scanTable{{table: bucket, base: bucketBase(bucket)}}, nil
|
||||
}
|
||||
tables := []scanTable{{table: DEFAULT_TABLE}}
|
||||
if dirPath != "/" && string(dirPath) != bucketsRoot {
|
||||
// Outside every bucket (e.g. /topics, or an internal /buckets/.system
|
||||
// folder): everything below dirPath is in the default table.
|
||||
return tables, nil
|
||||
}
|
||||
buckets, err := store.listBucketNames(ctx, tx, cachedBuckets)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, bucket := range buckets.names {
|
||||
tables = append(tables, scanTable{table: bucket, base: bucketBase(bucket), cachedOnly: !buckets.seen[bucket]})
|
||||
}
|
||||
return tables, nil
|
||||
}
|
||||
|
||||
const bucketsRoot = "/buckets"
|
||||
|
||||
func bucketBase(bucket string) string {
|
||||
return bucketsRoot + "/" + bucket
|
||||
}
|
||||
|
||||
// bucketTableFor returns the bucket whose table alone holds everything below
|
||||
// dirPath, or "" when dirPath is not inside a real bucket (as getTxOrDB routes).
|
||||
func bucketTableFor(dirPath util.FullPath) string {
|
||||
p := string(dirPath)
|
||||
if !strings.HasPrefix(p, bucketsRoot+"/") {
|
||||
return ""
|
||||
}
|
||||
rest := p[len(bucketsRoot)+1:]
|
||||
bucket := rest
|
||||
if i := strings.Index(rest, "/"); i >= 0 {
|
||||
bucket = rest[:i]
|
||||
}
|
||||
if bucket == "" || strings.HasPrefix(bucket, ".") || !isValidBucket(bucket) {
|
||||
return ""
|
||||
}
|
||||
return bucket
|
||||
}
|
||||
|
||||
// bucketNames is the snapshot-listed and cached-only buckets whose tables a
|
||||
// scan may read.
|
||||
type bucketNames struct {
|
||||
names []string
|
||||
seen map[string]bool // listed under /buckets inside the snapshot
|
||||
}
|
||||
|
||||
// listBucketNames reads the directories under /buckets within the snapshot
|
||||
// and keeps those that route to their own table, plus the buckets this
|
||||
// process has already seen (their directory entry may be gone, their rows not).
|
||||
func (store *AbstractSqlStore) listBucketNames(ctx context.Context, tx *sql.Tx, cachedBuckets []string) (bucketNames, error) {
|
||||
var names bucketNames
|
||||
names.seen = make(map[string]bool)
|
||||
added := make(map[string]bool)
|
||||
add := func(name string) {
|
||||
if added[name] || strings.HasPrefix(name, ".") || !isValidBucket(name) {
|
||||
return
|
||||
}
|
||||
added[name] = true
|
||||
names.names = append(names.names, name)
|
||||
}
|
||||
|
||||
const page = 1000
|
||||
startFrom := ""
|
||||
for {
|
||||
type row struct {
|
||||
name string
|
||||
data []byte
|
||||
}
|
||||
var rows []row
|
||||
if err := func() error {
|
||||
res, err := tx.QueryContext(ctx, store.GetSqlListExclusive(DEFAULT_TABLE), util.HashStringToLong(bucketsRoot), startFrom, bucketsRoot, "%", page)
|
||||
if err != nil {
|
||||
return fmt.Errorf("list %s: %v", bucketsRoot, err)
|
||||
}
|
||||
defer res.Close()
|
||||
for res.Next() {
|
||||
var r row
|
||||
if err := res.Scan(&r.name, &r.data); err != nil {
|
||||
return fmt.Errorf("scan %s: %v", bucketsRoot, err)
|
||||
}
|
||||
rows = append(rows, r)
|
||||
}
|
||||
return res.Err()
|
||||
}(); err != nil {
|
||||
return bucketNames{}, err
|
||||
}
|
||||
for _, r := range rows {
|
||||
startFrom = r.name
|
||||
entry := &filer.Entry{FullPath: util.NewFullPath(bucketsRoot, r.name)}
|
||||
if err := entry.DecodeAttributesAndChunks(util.MaybeDecompressData(r.data)); err != nil {
|
||||
return bucketNames{}, fmt.Errorf("scan decode %s: %v", entry.FullPath, err)
|
||||
}
|
||||
if entry.IsDirectory() {
|
||||
names.seen[r.name] = true
|
||||
add(r.name)
|
||||
}
|
||||
}
|
||||
if len(rows) < page {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
for _, name := range cachedBuckets {
|
||||
add(name)
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
// scanOneTable reads one table in primary-key pages and hands the entries
|
||||
// strictly below dirPath to fn (the root itself is read separately). Key-value
|
||||
// records share the default table; GenDirAndName stores the raw key bytes as
|
||||
// their dirhash while entry rows hash the directory, so the mismatch
|
||||
// identifies them exactly.
|
||||
func (store *AbstractSqlStore) scanOneTable(ctx context.Context, tx *sql.Tx, t scanTable, dirPath util.FullPath, fn func(ctx context.Context, entry *filer.Entry) error) error {
|
||||
type row struct {
|
||||
dirHash int64
|
||||
name, dir string
|
||||
data []byte
|
||||
}
|
||||
cursorHash, cursorName := int64(math.MinInt64), ""
|
||||
firstPage := true
|
||||
for {
|
||||
var page []row
|
||||
if t.cachedOnly && firstPage {
|
||||
// A missing table can only be met by the first page read: once a
|
||||
// row came back the tx holds the table against a drop. On
|
||||
// PostgreSQL the failed query aborts the transaction, so the skip
|
||||
// needs a savepoint to roll back to before the next table's read.
|
||||
if _, err := tx.ExecContext(ctx, "SAVEPOINT seaweed_scan_bucket"); err != nil {
|
||||
return fmt.Errorf("scan table %s: %v", t.table, err)
|
||||
}
|
||||
}
|
||||
sqlText, args := store.GetSqlScanPage(t.table, cursorHash, cursorName, scanPageSize)
|
||||
rows, err := tx.QueryContext(ctx, sqlText, args...)
|
||||
if err != nil {
|
||||
if t.cachedOnly && isMissingTableError(err) {
|
||||
tx.ExecContext(ctx, "ROLLBACK TO SAVEPOINT seaweed_scan_bucket")
|
||||
return nil // a cached-only name whose table was dropped meanwhile
|
||||
}
|
||||
return missingTableOr(err, t.table, fmt.Sprintf("scan table %s", t.table))
|
||||
}
|
||||
firstPage = false
|
||||
if err := func() error {
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var r row
|
||||
if err := rows.Scan(&r.dirHash, &r.name, &r.dir, &r.data); err != nil {
|
||||
return fmt.Errorf("scan table %s: %v", t.table, err)
|
||||
}
|
||||
page = append(page, r)
|
||||
}
|
||||
return rows.Err()
|
||||
}(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, r := range page {
|
||||
cursorHash, cursorName = r.dirHash, r.name
|
||||
if r.dirHash != util.HashStringToLong(r.dir) {
|
||||
continue // a key-value record, see GenDirAndName
|
||||
}
|
||||
fullPath := util.NewFullPath(t.base+r.dir, r.name)
|
||||
if !isStrictlyBelow(fullPath, dirPath) {
|
||||
continue
|
||||
}
|
||||
entry := &filer.Entry{FullPath: fullPath}
|
||||
if err := entry.DecodeAttributesAndChunks(util.MaybeDecompressData(r.data)); err != nil {
|
||||
return fmt.Errorf("scan decode %s: %v", fullPath, err)
|
||||
}
|
||||
if err := fn(ctx, entry); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(page) < scanPageSize {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// missingTableOr names the condition when a bucket's table is not there. The
|
||||
// scan fails closed: from inside the snapshot a table that was never created
|
||||
// cannot be told from one dropped a moment ago, whose rows the snapshot still
|
||||
// counts, and treating either as empty would let fsck purge referenced chunks.
|
||||
func missingTableOr(err error, table, what string) error {
|
||||
if table != DEFAULT_TABLE && isMissingTableError(err) {
|
||||
return fmt.Errorf("bucket %s has a directory entry but no table (never created, or dropped during the scan): %v", table, err)
|
||||
}
|
||||
return fmt.Errorf("%s: %v", what, err)
|
||||
}
|
||||
|
||||
// isMissingTableError recognizes a missing table by message, as InsertEntry
|
||||
// recognizes duplicates: PostgreSQL `relation "x" does not exist` (SQLSTATE
|
||||
// 42P01), MySQL error 1146 (SQLSTATE 42S02), SQLite "no such table".
|
||||
func isMissingTableError(err error) bool {
|
||||
msg := strings.ToLower(err.Error())
|
||||
return strings.Contains(msg, "42p01") ||
|
||||
(strings.Contains(msg, "relation \"") && strings.Contains(msg, "does not exist")) ||
|
||||
strings.Contains(msg, "42s02") ||
|
||||
strings.Contains(msg, "error 1146") ||
|
||||
strings.Contains(msg, "no such table")
|
||||
}
|
||||
|
||||
// isStrictlyBelow reports whether p is a descendant of dir (not dir itself).
|
||||
func isStrictlyBelow(p, dir util.FullPath) bool {
|
||||
if dir == "/" {
|
||||
return p != "/"
|
||||
}
|
||||
return strings.HasPrefix(string(p), string(dir)+"/")
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package abstract_sql
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
)
|
||||
|
||||
func TestBucketTableFor(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"/": "",
|
||||
"/buckets": "",
|
||||
"/buckets/": "",
|
||||
"/buckets/mybucket": "mybucket",
|
||||
"/buckets/mybucket/a/b": "mybucket",
|
||||
"/buckets/.system/owners": "",
|
||||
"/buckets/Invalid_Bucket/x": "",
|
||||
"/topics/.system/log": "",
|
||||
"/bucketsfoo/mybucket": "",
|
||||
}
|
||||
for path, want := range cases {
|
||||
if got := bucketTableFor(util.FullPath(path)); got != want {
|
||||
t.Errorf("bucketTableFor(%s) = %q, want %q", path, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Only a missing table may be skipped; any other schema error must fail the
|
||||
// scan, or a bucket's live chunks would be reported as orphans.
|
||||
func TestIsMissingTableError(t *testing.T) {
|
||||
for _, msg := range []string{
|
||||
`ERROR: relation "ghost" does not exist (SQLSTATE 42P01)`,
|
||||
`Error 1146 (42S02): Table 'seaweedfs.ghost' doesn't exist`,
|
||||
`SQL logic error: no such table: ghost (1)`,
|
||||
} {
|
||||
if !isMissingTableError(errors.New(msg)) {
|
||||
t.Errorf("not recognized as a missing table: %s", msg)
|
||||
}
|
||||
}
|
||||
for _, msg := range []string{
|
||||
`ERROR: column "directory" does not exist (SQLSTATE 42703)`,
|
||||
`Error 1054 (42S22): Unknown column 'directory' in 'field list'`,
|
||||
`SQL logic error: no such column: directory (1)`,
|
||||
"connection refused",
|
||||
} {
|
||||
if isMissingTableError(errors.New(msg)) {
|
||||
t.Errorf("wrongly taken for a missing table: %s", msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -413,6 +413,28 @@ func (f *Filer) RollbackTransaction(ctx context.Context) error {
|
||||
return f.Store.RollbackTransaction(ctx)
|
||||
}
|
||||
|
||||
type syncWriteKey struct{}
|
||||
|
||||
// WithSyncWrite marks the entry writes under ctx durable-on-ack: a store that
|
||||
// buffers writes by default must flush them before returning.
|
||||
func WithSyncWrite(ctx context.Context) context.Context {
|
||||
return context.WithValue(ctx, syncWriteKey{}, true)
|
||||
}
|
||||
|
||||
// SyncWrite reports whether ctx carries a durability request for entry writes.
|
||||
func SyncWrite(ctx context.Context) bool {
|
||||
return ctx.Value(syncWriteKey{}) != nil
|
||||
}
|
||||
|
||||
// syncWriteContext flags ctx when the storage rule on p asks for fsync, so the
|
||||
// store write is held to the same durability as the data path.
|
||||
func (f *Filer) syncWriteContext(ctx context.Context, p util.FullPath) context.Context {
|
||||
if f.FilerConf != nil && f.FilerConf.MatchStorageRule(string(p)).Fsync {
|
||||
return WithSyncWrite(ctx)
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
// CreateEntry creates or replaces an entry. When existing is non-nil the caller
|
||||
// has already fetched the current entry at this path under a path lock, and it
|
||||
// is reused instead of looking the store up again; pass nil to have CreateEntry
|
||||
@@ -423,6 +445,8 @@ func (f *Filer) CreateEntry(ctx context.Context, entry *Entry, existing *Entry,
|
||||
return nil
|
||||
}
|
||||
|
||||
ctx = f.syncWriteContext(ctx, entry.FullPath)
|
||||
|
||||
if entry.FullPath.IsLongerFileName(maxFilenameLength) {
|
||||
return filer_pb.ErrEntryNameTooLong
|
||||
}
|
||||
@@ -628,6 +652,7 @@ func (f *Filer) DirectoryAttributes(ctx context.Context, dirPath util.FullPath)
|
||||
// deleted with, so a restore cannot hand back a directory more permissive than the one
|
||||
// it replaces - including when someone else already put it back with wider ones.
|
||||
func (f *Filer) EnsureDirectoryEntry(ctx context.Context, dirPath util.FullPath, attrs empty_folder_cleanup.DirectoryAttributes) error {
|
||||
ctx = f.syncWriteContext(ctx, dirPath)
|
||||
existing, err := f.FindEntry(ctx, dirPath)
|
||||
if err != nil && !errors.Is(err, filer_pb.ErrNotFound) {
|
||||
return err
|
||||
@@ -677,6 +702,7 @@ func (f *Filer) EnsureDirectoryEntry(ctx context.Context, dirPath util.FullPath,
|
||||
}
|
||||
|
||||
func (f *Filer) UpdateEntry(ctx context.Context, oldEntry, entry *Entry, isFromOtherCluster bool) (err error) {
|
||||
ctx = f.syncWriteContext(ctx, entry.FullPath)
|
||||
if oldEntry != nil {
|
||||
entry.Attr.Crtime = oldEntry.Attr.Crtime
|
||||
if oldEntry.Attr.Inode != 0 {
|
||||
|
||||
@@ -122,6 +122,8 @@ func (f *Filer) DeleteEntryMetaAndData(ctx context.Context, p util.FullPath, isR
|
||||
return nil
|
||||
}
|
||||
|
||||
ctx = f.syncWriteContext(ctx, p)
|
||||
|
||||
entry, findErr := f.FindEntry(ctx, p)
|
||||
if findErr != nil {
|
||||
return findErr
|
||||
|
||||
@@ -44,6 +44,22 @@ type FilerStore interface {
|
||||
Shutdown()
|
||||
}
|
||||
|
||||
// ErrUnsupportedEntryScan is returned by EntryScanner.ScanEntries, before
|
||||
// any entry is visited, when the store cannot serve the scan.
|
||||
var ErrUnsupportedEntryScan = errors.New("entry scan not supported by this filer store")
|
||||
|
||||
// EntryScanner is an optional FilerStore capability: visit every entry at and
|
||||
// below dirPath, in any order, from one consistent snapshot of the store.
|
||||
//
|
||||
// A directory walk sees each directory at a different time, so an entry
|
||||
// renamed from a directory not yet listed into one already listed is never
|
||||
// seen; volume.fsck then purges its chunks as orphans. A snapshot sees it
|
||||
// exactly once. The context handed to fn carries the snapshot, so store reads
|
||||
// made with it (hard-link records) see the same instant.
|
||||
type EntryScanner interface {
|
||||
ScanEntries(ctx context.Context, dirPath util.FullPath, fn func(ctx context.Context, entry *Entry) error) error
|
||||
}
|
||||
|
||||
type BucketAware interface {
|
||||
OnBucketCreation(bucket string)
|
||||
OnBucketDeletion(bucket string)
|
||||
|
||||
@@ -2,6 +2,7 @@ package filer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
@@ -19,6 +20,7 @@ import (
|
||||
var (
|
||||
_ = VirtualFilerStore(&FilerStoreWrapper{})
|
||||
_ = Debuggable(&FilerStoreWrapper{})
|
||||
_ = EntryScanner(&FilerStoreWrapper{})
|
||||
)
|
||||
|
||||
type VirtualFilerStore interface {
|
||||
@@ -364,6 +366,33 @@ func (fsw *FilerStoreWrapper) ListDirectoryPrefixedEntries(ctx context.Context,
|
||||
return lastFileName, err
|
||||
}
|
||||
|
||||
// ScanEntries delegates to the default store. With path-specific stores
|
||||
// configured, entries below dirPath could live in several stores and one
|
||||
// snapshot per store is not one snapshot, so the scan is declined. Hard links
|
||||
// are resolved inside the snapshot; a store error fails the scan (the per-path
|
||||
// copy can be stale), a missing record falls back to it as listings do.
|
||||
func (fsw *FilerStoreWrapper) ScanEntries(ctx context.Context, dirPath util.FullPath, fn func(ctx context.Context, entry *Entry) error) error {
|
||||
if fsw.hasPathSpecificStore {
|
||||
return ErrUnsupportedEntryScan
|
||||
}
|
||||
scanner, ok := fsw.defaultStore.(EntryScanner)
|
||||
if !ok {
|
||||
return ErrUnsupportedEntryScan
|
||||
}
|
||||
stats.FilerStoreCounter.WithLabelValues(fsw.defaultStore.GetName(), "scan").Inc()
|
||||
start := time.Now()
|
||||
defer func() {
|
||||
stats.FilerStoreHistogram.WithLabelValues(fsw.defaultStore.GetName(), "scan").Observe(time.Since(start).Seconds())
|
||||
}()
|
||||
return scanner.ScanEntries(ctx, dirPath, func(scanCtx context.Context, entry *Entry) error {
|
||||
if err := fsw.maybeReadHardLink(scanCtx, entry); err != nil && !errors.Is(err, ErrKvNotFound) {
|
||||
return fmt.Errorf("resolve hard link of %s: %w", entry.FullPath, err)
|
||||
}
|
||||
filer_pb.AfterEntryDeserialization(entry.GetChunks())
|
||||
return fn(scanCtx, entry)
|
||||
})
|
||||
}
|
||||
|
||||
func (fsw *FilerStoreWrapper) prefixFilterEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc ListEachEntryFunc) (lastFileName string, err error) {
|
||||
actualStore := fsw.getActualStore(dirPath + "/")
|
||||
|
||||
|
||||
@@ -79,6 +79,15 @@ func (store *LevelDBStore) RollbackTransaction(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var syncWriteOptions = &opt.WriteOptions{Sync: true}
|
||||
|
||||
func writeOptions(ctx context.Context) *opt.WriteOptions {
|
||||
if filer.SyncWrite(ctx) {
|
||||
return syncWriteOptions
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *LevelDBStore) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
||||
key := genKey(entry.DirAndName())
|
||||
|
||||
@@ -91,7 +100,7 @@ func (store *LevelDBStore) InsertEntry(ctx context.Context, entry *filer.Entry)
|
||||
value = weed_util.MaybeGzipData(value)
|
||||
}
|
||||
|
||||
err = store.db.Put(key, value, nil)
|
||||
err = store.db.Put(key, value, writeOptions(ctx))
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
|
||||
@@ -168,7 +177,7 @@ func (store *LevelDBStore) FindEntry(ctx context.Context, fullpath weed_util.Ful
|
||||
func (store *LevelDBStore) DeleteEntry(ctx context.Context, fullpath weed_util.FullPath) (err error) {
|
||||
key := genKey(fullpath.DirAndName())
|
||||
|
||||
err = store.db.Delete(key, nil)
|
||||
err = store.db.Delete(key, writeOptions(ctx))
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete %s : %v", fullpath, err)
|
||||
}
|
||||
@@ -195,7 +204,7 @@ func (store *LevelDBStore) DeleteFolderChildren(ctx context.Context, fullpath we
|
||||
}
|
||||
iter.Release()
|
||||
|
||||
err = store.db.Write(batch, nil)
|
||||
err = store.db.Write(batch, writeOptions(ctx))
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete %s : %v", fullpath, err)
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"os"
|
||||
|
||||
"github.com/syndtr/goleveldb/leveldb"
|
||||
@@ -28,6 +29,11 @@ type LevelDB2Store struct {
|
||||
dbs []*leveldb.DB
|
||||
dbCount int
|
||||
ReadOnly bool
|
||||
|
||||
blockCacheSizeMB int
|
||||
writeBufferSizeMB int
|
||||
blockSizeKB int
|
||||
compactionTableSizeMB int
|
||||
}
|
||||
|
||||
func (store *LevelDB2Store) GetName() string {
|
||||
@@ -36,9 +42,54 @@ func (store *LevelDB2Store) GetName() string {
|
||||
|
||||
func (store *LevelDB2Store) Initialize(configuration weed_util.Configuration, prefix string) (err error) {
|
||||
dir := configuration.GetString(prefix + "dir")
|
||||
store.blockCacheSizeMB = configuration.GetInt(prefix + "block_cache_size_mb")
|
||||
store.writeBufferSizeMB = configuration.GetInt(prefix + "write_buffer_size_mb")
|
||||
store.blockSizeKB = configuration.GetInt(prefix + "block_size_kb")
|
||||
store.compactionTableSizeMB = configuration.GetInt(prefix + "compaction_table_size_mb")
|
||||
if err := store.validateSizeOptions(); err != nil {
|
||||
return err
|
||||
}
|
||||
return store.initialize(dir, 8)
|
||||
}
|
||||
|
||||
// validateSizeOptions rejects sizes too large to convert to bytes without
|
||||
// overflowing int on this platform.
|
||||
func (store *LevelDB2Store) validateSizeOptions() error {
|
||||
for name, kv := range map[string][2]int{
|
||||
"block_cache_size_mb": {store.blockCacheSizeMB, 1 << 20},
|
||||
"write_buffer_size_mb": {store.writeBufferSizeMB, 1 << 20},
|
||||
"block_size_kb": {store.blockSizeKB, 1 << 10},
|
||||
"compaction_table_size_mb": {store.compactionTableSizeMB, 1 << 20},
|
||||
} {
|
||||
if kv[0] > math.MaxInt/kv[1] {
|
||||
return fmt.Errorf("leveldb2 option %s=%d overflows int", name, kv[0])
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *LevelDB2Store) buildOptions() *opt.Options {
|
||||
opts := &opt.Options{
|
||||
BlockCacheCapacity: 32 * 1024 * 1024, // default value is 8MiB
|
||||
WriteBuffer: 16 * 1024 * 1024, // default value is 4MiB
|
||||
Filter: filter.NewBloomFilter(8), // false positive rate 0.02
|
||||
ReadOnly: store.ReadOnly,
|
||||
}
|
||||
if store.blockCacheSizeMB > 0 {
|
||||
opts.BlockCacheCapacity = store.blockCacheSizeMB * 1024 * 1024
|
||||
}
|
||||
if store.writeBufferSizeMB > 0 {
|
||||
opts.WriteBuffer = store.writeBufferSizeMB * 1024 * 1024
|
||||
}
|
||||
if store.blockSizeKB > 0 {
|
||||
opts.BlockSize = store.blockSizeKB * 1024
|
||||
}
|
||||
if store.compactionTableSizeMB > 0 {
|
||||
opts.CompactionTableSize = store.compactionTableSizeMB * 1024 * 1024
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
func (store *LevelDB2Store) initialize(dir string, dbCount int) (err error) {
|
||||
glog.Infof("filer store leveldb2 dir: %s", dir)
|
||||
os.MkdirAll(dir, 0755)
|
||||
@@ -46,12 +97,7 @@ func (store *LevelDB2Store) initialize(dir string, dbCount int) (err error) {
|
||||
return fmt.Errorf("Check Level Folder %s Writable: %s", dir, err)
|
||||
}
|
||||
|
||||
opts := &opt.Options{
|
||||
BlockCacheCapacity: 32 * 1024 * 1024, // default value is 8MiB
|
||||
WriteBuffer: 16 * 1024 * 1024, // default value is 4MiB
|
||||
Filter: filter.NewBloomFilter(8), // false positive rate 0.02
|
||||
ReadOnly: store.ReadOnly,
|
||||
}
|
||||
opts := store.buildOptions()
|
||||
|
||||
for d := 0; d < dbCount; d++ {
|
||||
dbFolder := fmt.Sprintf("%s/%02d", dir, d)
|
||||
@@ -81,6 +127,15 @@ func (store *LevelDB2Store) RollbackTransaction(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var syncWriteOptions = &opt.WriteOptions{Sync: true}
|
||||
|
||||
func writeOptions(ctx context.Context) *opt.WriteOptions {
|
||||
if filer.SyncWrite(ctx) {
|
||||
return syncWriteOptions
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *LevelDB2Store) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
||||
dir, name := entry.DirAndName()
|
||||
key, partitionId := genKey(dir, name, store.dbCount)
|
||||
@@ -94,7 +149,7 @@ func (store *LevelDB2Store) InsertEntry(ctx context.Context, entry *filer.Entry)
|
||||
value = weed_util.MaybeGzipData(value)
|
||||
}
|
||||
|
||||
err = store.dbs[partitionId].Put(key, value, nil)
|
||||
err = store.dbs[partitionId].Put(key, value, writeOptions(ctx))
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
|
||||
@@ -140,7 +195,7 @@ func (store *LevelDB2Store) DeleteEntry(ctx context.Context, fullpath weed_util.
|
||||
dir, name := fullpath.DirAndName()
|
||||
key, partitionId := genKey(dir, name, store.dbCount)
|
||||
|
||||
err = store.dbs[partitionId].Delete(key, nil)
|
||||
err = store.dbs[partitionId].Delete(key, writeOptions(ctx))
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete %s : %v", fullpath, err)
|
||||
}
|
||||
@@ -167,7 +222,7 @@ func (store *LevelDB2Store) DeleteFolderChildren(ctx context.Context, fullpath w
|
||||
}
|
||||
iter.Release()
|
||||
|
||||
err = store.dbs[partitionId].Write(batch, nil)
|
||||
err = store.dbs[partitionId].Write(batch, writeOptions(ctx))
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete %s : %v", fullpath, err)
|
||||
|
||||
@@ -2,6 +2,8 @@ package leveldb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer"
|
||||
@@ -62,6 +64,93 @@ func TestCreateAndFind(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
type fakeConfiguration map[string]interface{}
|
||||
|
||||
func (c fakeConfiguration) GetString(key string) string {
|
||||
value, _ := c[key].(string)
|
||||
return value
|
||||
}
|
||||
|
||||
func (c fakeConfiguration) GetBool(key string) bool {
|
||||
value, _ := c[key].(bool)
|
||||
return value
|
||||
}
|
||||
|
||||
func (c fakeConfiguration) GetInt(key string) int {
|
||||
value, _ := c[key].(int)
|
||||
return value
|
||||
}
|
||||
|
||||
func (c fakeConfiguration) GetStringSlice(key string) []string {
|
||||
value, _ := c[key].([]string)
|
||||
return value
|
||||
}
|
||||
|
||||
func (c fakeConfiguration) SetDefault(key string, value interface{}) {
|
||||
if _, found := c[key]; !found {
|
||||
c[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializeOptions(t *testing.T) {
|
||||
store := &LevelDB2Store{}
|
||||
if err := store.Initialize(fakeConfiguration{
|
||||
"leveldb2.dir": t.TempDir(),
|
||||
"leveldb2.block_cache_size_mb": 64,
|
||||
"leveldb2.write_buffer_size_mb": 32,
|
||||
"leveldb2.block_size_kb": 16,
|
||||
"leveldb2.compaction_table_size_mb": 8,
|
||||
}, "leveldb2."); err != nil {
|
||||
t.Fatalf("initialize: %v", err)
|
||||
}
|
||||
defer store.Shutdown()
|
||||
|
||||
opts := store.buildOptions()
|
||||
if opts.BlockCacheCapacity != 64*1024*1024 {
|
||||
t.Errorf("block cache %d, want 64MiB", opts.BlockCacheCapacity)
|
||||
}
|
||||
if opts.WriteBuffer != 32*1024*1024 {
|
||||
t.Errorf("write buffer %d, want 32MiB", opts.WriteBuffer)
|
||||
}
|
||||
if opts.BlockSize != 16*1024 {
|
||||
t.Errorf("block size %d, want 16KiB", opts.BlockSize)
|
||||
}
|
||||
if opts.CompactionTableSize != 8*1024*1024 {
|
||||
t.Errorf("compaction table size %d, want 8MiB", opts.CompactionTableSize)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializeDefaultOptions(t *testing.T) {
|
||||
store := &LevelDB2Store{}
|
||||
if err := store.Initialize(fakeConfiguration{
|
||||
"leveldb2.dir": t.TempDir(),
|
||||
"leveldb2.block_cache_size_mb": -1,
|
||||
"leveldb2.write_buffer_size_mb": 0,
|
||||
}, "leveldb2."); err != nil {
|
||||
t.Fatalf("initialize: %v", err)
|
||||
}
|
||||
defer store.Shutdown()
|
||||
|
||||
opts := store.buildOptions()
|
||||
if opts.BlockCacheCapacity != 32*1024*1024 {
|
||||
t.Errorf("block cache %d, want the 32MiB default", opts.BlockCacheCapacity)
|
||||
}
|
||||
if opts.WriteBuffer != 16*1024*1024 {
|
||||
t.Errorf("write buffer %d, want the 16MiB default", opts.WriteBuffer)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializeRejectsOversizedOption(t *testing.T) {
|
||||
store := &LevelDB2Store{}
|
||||
err := store.Initialize(fakeConfiguration{
|
||||
"leveldb2.dir": t.TempDir(),
|
||||
"leveldb2.write_buffer_size_mb": math.MaxInt/(1<<20) + 1,
|
||||
}, "leveldb2.")
|
||||
if err == nil || !strings.Contains(err.Error(), "write_buffer_size_mb") {
|
||||
t.Fatalf("expected overflow rejection, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyRoot(t *testing.T) {
|
||||
testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", "", "", "", "", 255, nil)
|
||||
dir := t.TempDir()
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -35,6 +36,11 @@ type LevelDB3Store struct {
|
||||
dbs map[string]*leveldb.DB
|
||||
dbsLock sync.RWMutex
|
||||
ReadOnly bool
|
||||
|
||||
blockCacheSizeMB int
|
||||
writeBufferSizeMB int
|
||||
blockSizeKB int
|
||||
compactionTableSizeMB int
|
||||
}
|
||||
|
||||
func (store *LevelDB3Store) GetName() string {
|
||||
@@ -43,9 +49,58 @@ func (store *LevelDB3Store) GetName() string {
|
||||
|
||||
func (store *LevelDB3Store) Initialize(configuration weed_util.Configuration, prefix string) (err error) {
|
||||
dir := configuration.GetString(prefix + "dir")
|
||||
store.blockCacheSizeMB = configuration.GetInt(prefix + "block_cache_size_mb")
|
||||
store.writeBufferSizeMB = configuration.GetInt(prefix + "write_buffer_size_mb")
|
||||
store.blockSizeKB = configuration.GetInt(prefix + "block_size_kb")
|
||||
store.compactionTableSizeMB = configuration.GetInt(prefix + "compaction_table_size_mb")
|
||||
if err := store.validateSizeOptions(); err != nil {
|
||||
return err
|
||||
}
|
||||
return store.initialize(dir)
|
||||
}
|
||||
|
||||
// validateSizeOptions rejects sizes too large to convert to bytes without
|
||||
// overflowing int on this platform.
|
||||
func (store *LevelDB3Store) validateSizeOptions() error {
|
||||
for name, kv := range map[string][2]int{
|
||||
"block_cache_size_mb": {store.blockCacheSizeMB, 1 << 20},
|
||||
"write_buffer_size_mb": {store.writeBufferSizeMB, 1 << 20},
|
||||
"block_size_kb": {store.blockSizeKB, 1 << 10},
|
||||
"compaction_table_size_mb": {store.compactionTableSizeMB, 1 << 20},
|
||||
} {
|
||||
if kv[0] > math.MaxInt/kv[1] {
|
||||
return fmt.Errorf("leveldb3 option %s=%d overflows int", name, kv[0])
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *LevelDB3Store) buildOptions(name string) *opt.Options {
|
||||
opts := &opt.Options{
|
||||
BlockCacheCapacity: 32 * 1024 * 1024, // default value is 8MiB
|
||||
WriteBuffer: 16 * 1024 * 1024, // default value is 4MiB
|
||||
Filter: filter.NewBloomFilter(8),
|
||||
ReadOnly: store.ReadOnly,
|
||||
}
|
||||
if name != DEFAULT {
|
||||
opts.BlockCacheCapacity = 16 * 1024 * 1024 // default value is 8MiB
|
||||
opts.WriteBuffer = 8 * 1024 * 1024 // default value is 4MiB
|
||||
}
|
||||
if store.blockCacheSizeMB > 0 {
|
||||
opts.BlockCacheCapacity = store.blockCacheSizeMB * 1024 * 1024
|
||||
}
|
||||
if store.writeBufferSizeMB > 0 {
|
||||
opts.WriteBuffer = store.writeBufferSizeMB * 1024 * 1024
|
||||
}
|
||||
if store.blockSizeKB > 0 {
|
||||
opts.BlockSize = store.blockSizeKB * 1024
|
||||
}
|
||||
if store.compactionTableSizeMB > 0 {
|
||||
opts.CompactionTableSize = store.compactionTableSizeMB * 1024 * 1024
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
func (store *LevelDB3Store) initialize(dir string) (err error) {
|
||||
glog.Infof("filer store leveldb3 dir: %s", dir)
|
||||
os.MkdirAll(dir, 0755)
|
||||
@@ -65,21 +120,7 @@ func (store *LevelDB3Store) initialize(dir string) (err error) {
|
||||
}
|
||||
|
||||
func (store *LevelDB3Store) loadDB(name string) (*leveldb.DB, error) {
|
||||
bloom := filter.NewBloomFilter(8) // false positive rate 0.02
|
||||
opts := &opt.Options{
|
||||
BlockCacheCapacity: 32 * 1024 * 1024, // default value is 8MiB
|
||||
WriteBuffer: 16 * 1024 * 1024, // default value is 4MiB
|
||||
Filter: bloom,
|
||||
ReadOnly: store.ReadOnly,
|
||||
}
|
||||
if name != DEFAULT {
|
||||
opts = &opt.Options{
|
||||
BlockCacheCapacity: 16 * 1024 * 1024, // default value is 8MiB
|
||||
WriteBuffer: 8 * 1024 * 1024, // default value is 4MiB
|
||||
Filter: bloom,
|
||||
ReadOnly: store.ReadOnly,
|
||||
}
|
||||
}
|
||||
opts := store.buildOptions(name)
|
||||
|
||||
dbFolder := fmt.Sprintf("%s/%s", store.dir, name)
|
||||
os.MkdirAll(dbFolder, 0755)
|
||||
@@ -180,6 +221,15 @@ func (store *LevelDB3Store) RollbackTransaction(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var syncWriteOptions = &opt.WriteOptions{Sync: true}
|
||||
|
||||
func writeOptions(ctx context.Context) *opt.WriteOptions {
|
||||
if filer.SyncWrite(ctx) {
|
||||
return syncWriteOptions
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *LevelDB3Store) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
||||
|
||||
db, _, shortPath, err := store.findDB(entry.FullPath, false)
|
||||
@@ -199,7 +249,7 @@ func (store *LevelDB3Store) InsertEntry(ctx context.Context, entry *filer.Entry)
|
||||
value = weed_util.MaybeGzipData(value)
|
||||
}
|
||||
|
||||
err = db.Put(key, value, nil)
|
||||
err = db.Put(key, value, writeOptions(ctx))
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
|
||||
@@ -257,7 +307,7 @@ func (store *LevelDB3Store) DeleteEntry(ctx context.Context, fullpath weed_util.
|
||||
dir, name := shortPath.DirAndName()
|
||||
key := genKey(dir, name)
|
||||
|
||||
err = db.Delete(key, nil)
|
||||
err = db.Delete(key, writeOptions(ctx))
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete %s : %v", fullpath, err)
|
||||
}
|
||||
@@ -298,7 +348,7 @@ func (store *LevelDB3Store) DeleteFolderChildren(ctx context.Context, fullpath w
|
||||
}
|
||||
iter.Release()
|
||||
|
||||
err = db.Write(batch, nil)
|
||||
err = db.Write(batch, writeOptions(ctx))
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete %s : %v", fullpath, err)
|
||||
|
||||
@@ -2,6 +2,8 @@ package leveldb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer"
|
||||
@@ -62,6 +64,103 @@ func TestCreateAndFind(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
type fakeConfiguration map[string]interface{}
|
||||
|
||||
func (c fakeConfiguration) GetString(key string) string {
|
||||
value, _ := c[key].(string)
|
||||
return value
|
||||
}
|
||||
|
||||
func (c fakeConfiguration) GetBool(key string) bool {
|
||||
value, _ := c[key].(bool)
|
||||
return value
|
||||
}
|
||||
|
||||
func (c fakeConfiguration) GetInt(key string) int {
|
||||
value, _ := c[key].(int)
|
||||
return value
|
||||
}
|
||||
|
||||
func (c fakeConfiguration) GetStringSlice(key string) []string {
|
||||
value, _ := c[key].([]string)
|
||||
return value
|
||||
}
|
||||
|
||||
func (c fakeConfiguration) SetDefault(key string, value interface{}) {
|
||||
if _, found := c[key]; !found {
|
||||
c[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializeOptions(t *testing.T) {
|
||||
store := &LevelDB3Store{}
|
||||
if err := store.Initialize(fakeConfiguration{
|
||||
"leveldb3.dir": t.TempDir(),
|
||||
"leveldb3.block_cache_size_mb": 64,
|
||||
"leveldb3.write_buffer_size_mb": 32,
|
||||
"leveldb3.block_size_kb": 16,
|
||||
"leveldb3.compaction_table_size_mb": 8,
|
||||
}, "leveldb3."); err != nil {
|
||||
t.Fatalf("initialize: %v", err)
|
||||
}
|
||||
defer store.Shutdown()
|
||||
|
||||
for _, name := range []string{DEFAULT, "bucket-a"} {
|
||||
opts := store.buildOptions(name)
|
||||
if opts.BlockCacheCapacity != 64*1024*1024 {
|
||||
t.Errorf("%s block cache %d, want 64MiB", name, opts.BlockCacheCapacity)
|
||||
}
|
||||
if opts.WriteBuffer != 32*1024*1024 {
|
||||
t.Errorf("%s write buffer %d, want 32MiB", name, opts.WriteBuffer)
|
||||
}
|
||||
if opts.BlockSize != 16*1024 {
|
||||
t.Errorf("%s block size %d, want 16KiB", name, opts.BlockSize)
|
||||
}
|
||||
if opts.CompactionTableSize != 8*1024*1024 {
|
||||
t.Errorf("%s compaction table size %d, want 8MiB", name, opts.CompactionTableSize)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializeDefaultOptions(t *testing.T) {
|
||||
store := &LevelDB3Store{}
|
||||
if err := store.Initialize(fakeConfiguration{
|
||||
"leveldb3.dir": t.TempDir(),
|
||||
"leveldb3.block_cache_size_mb": -1,
|
||||
"leveldb3.write_buffer_size_mb": 0,
|
||||
}, "leveldb3."); err != nil {
|
||||
t.Fatalf("initialize: %v", err)
|
||||
}
|
||||
defer store.Shutdown()
|
||||
|
||||
opts := store.buildOptions(DEFAULT)
|
||||
if opts.BlockCacheCapacity != 32*1024*1024 {
|
||||
t.Errorf("block cache %d, want the 32MiB default", opts.BlockCacheCapacity)
|
||||
}
|
||||
if opts.WriteBuffer != 16*1024*1024 {
|
||||
t.Errorf("write buffer %d, want the 16MiB default", opts.WriteBuffer)
|
||||
}
|
||||
|
||||
bucketOpts := store.buildOptions("bucket-a")
|
||||
if bucketOpts.BlockCacheCapacity != 16*1024*1024 {
|
||||
t.Errorf("bucket block cache %d, want the 16MiB default", bucketOpts.BlockCacheCapacity)
|
||||
}
|
||||
if bucketOpts.WriteBuffer != 8*1024*1024 {
|
||||
t.Errorf("bucket write buffer %d, want the 8MiB default", bucketOpts.WriteBuffer)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializeRejectsOversizedOption(t *testing.T) {
|
||||
store := &LevelDB3Store{}
|
||||
err := store.Initialize(fakeConfiguration{
|
||||
"leveldb3.dir": t.TempDir(),
|
||||
"leveldb3.write_buffer_size_mb": math.MaxInt/(1<<20) + 1,
|
||||
}, "leveldb3.")
|
||||
if err == nil || !strings.Contains(err.Error(), "write_buffer_size_mb") {
|
||||
t.Fatalf("expected overflow rejection, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyRoot(t *testing.T) {
|
||||
testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", "", "", "", "", 255, nil)
|
||||
dir := t.TempDir()
|
||||
|
||||
@@ -62,6 +62,15 @@ func (gen *SqlGenMysql) nameExpr() string {
|
||||
return "`name`"
|
||||
}
|
||||
|
||||
// GetSqlScanPage spells the cursor as two primary-key ranges: MySQL 5.7 does
|
||||
// not turn a row-value comparison into a range seek. The name keeps the
|
||||
// column's own collation (BINARY would filesort every page); the scan promises
|
||||
// no lexical order. SQLite overrides this method.
|
||||
func (gen *SqlGenMysql) GetSqlScanPage(tableName string, dirhash int64, name string, limit int) (string, []interface{}) {
|
||||
return fmt.Sprintf("SELECT `dirhash`, `name`, `directory`, `meta` FROM `%s` WHERE `dirhash` > ? OR (`dirhash` = ? AND `name` > ?) ORDER BY `dirhash` ASC, `name` ASC LIMIT ?", tableName),
|
||||
[]interface{}{dirhash, dirhash, name, limit}
|
||||
}
|
||||
|
||||
func (gen *SqlGenMysql) GetSqlListExclusive(tableName string) string {
|
||||
name := gen.nameExpr()
|
||||
return fmt.Sprintf("SELECT `name`, `meta` FROM `%s` WHERE `dirhash` = ? AND %s > ? AND `directory` = ? AND %s LIKE ? ESCAPE '!' ORDER BY %s ASC LIMIT ?", tableName, name, name, name)
|
||||
|
||||
@@ -75,6 +75,14 @@ func (gen *SqlGenPostgres) nameExpr() string {
|
||||
return "name"
|
||||
}
|
||||
|
||||
// GetSqlScanPage uses a row-value comparison, one primary-key range seek on
|
||||
// PostgreSQL. The name keeps the column's own collation (COLLATE "C" would
|
||||
// sort every page); the scan promises no lexical order.
|
||||
func (gen *SqlGenPostgres) GetSqlScanPage(tableName string, dirhash int64, name string, limit int) (string, []interface{}) {
|
||||
return fmt.Sprintf(`SELECT dirhash, name, directory, meta FROM "%s" WHERE (dirhash, name) > ($1, $2) ORDER BY dirhash ASC, name ASC LIMIT $3`, tableName),
|
||||
[]interface{}{dirhash, name, limit}
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlListExclusive(tableName string) string {
|
||||
name := gen.nameExpr()
|
||||
return fmt.Sprintf(`SELECT NAME, meta FROM "%s" WHERE dirhash=$1 AND %s>$2 AND directory=$3 AND %s like $4 ESCAPE '!' ORDER BY %s ASC LIMIT $5`, tableName, name, name, name)
|
||||
|
||||
@@ -30,6 +30,31 @@ func (store *SqliteStore) GetName() string {
|
||||
return "sqlite"
|
||||
}
|
||||
|
||||
// sqliteSqlGen is the MySQL generator with SQLite's own page cursor: SQLite
|
||||
// seeks a row-value comparison on the primary key but scans the OR form.
|
||||
type sqliteSqlGen struct {
|
||||
*mysql.SqlGenMysql
|
||||
}
|
||||
|
||||
func (gen sqliteSqlGen) GetSqlScanPage(tableName string, dirhash int64, name string, limit int) (string, []interface{}) {
|
||||
return fmt.Sprintf("SELECT `dirhash`, `name`, `directory`, `meta` FROM `%s` WHERE (`dirhash`, `name`) > (?, ?) ORDER BY `dirhash` ASC, `name` ASC LIMIT ?", tableName),
|
||||
[]interface{}{dirhash, name, limit}
|
||||
}
|
||||
|
||||
// ScanEntries offers the snapshot scan only in WAL mode: with a rollback
|
||||
// journal a long read transaction holds a shared lock and writers fail after
|
||||
// the busy timeout.
|
||||
func (store *SqliteStore) ScanEntries(ctx context.Context, dirPath util.FullPath, fn func(ctx context.Context, entry *filer.Entry) error) error {
|
||||
var mode string
|
||||
if err := store.DB.QueryRowContext(ctx, "PRAGMA journal_mode").Scan(&mode); err != nil {
|
||||
return fmt.Errorf("%w: journal_mode: %v", filer.ErrUnsupportedEntryScan, err)
|
||||
}
|
||||
if !strings.EqualFold(mode, "wal") {
|
||||
return fmt.Errorf("%w: sqlite journal_mode is %q, the scan needs wal", filer.ErrUnsupportedEntryScan, mode)
|
||||
}
|
||||
return store.AbstractSqlStore.ScanEntries(ctx, dirPath, fn)
|
||||
}
|
||||
|
||||
func (store *SqliteStore) Initialize(configuration util.Configuration, prefix string) (err error) {
|
||||
dbFile := configuration.GetString(prefix + "dbFile")
|
||||
createTable := `CREATE TABLE IF NOT EXISTS "%s" (
|
||||
@@ -69,11 +94,11 @@ func sqliteDSN(dbFile string) string {
|
||||
func (store *SqliteStore) initialize(dbFile, createTable, upsertQuery string) (err error) {
|
||||
|
||||
store.SupportBucketTable = true
|
||||
store.SqlGenerator = &mysql.SqlGenMysql{
|
||||
store.SqlGenerator = sqliteSqlGen{&mysql.SqlGenMysql{
|
||||
CreateTableSqlTemplate: createTable,
|
||||
DropTableSqlTemplate: "drop table if exists `%s`",
|
||||
UpsertQueryTemplate: upsertQuery,
|
||||
}
|
||||
}}
|
||||
|
||||
dsn := sqliteDSN(dbFile)
|
||||
|
||||
@@ -94,6 +119,11 @@ func (store *SqliteStore) initialize(dbFile, createTable, upsertQuery string) (e
|
||||
}, 1, 1, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
// The main pool is one connection, which a long scan must not hold.
|
||||
if store.ScanDB, err = sql.Open("sqlite", dsn); err != nil {
|
||||
return fmt.Errorf("open scan connection to %s: %v", dbFile, err)
|
||||
}
|
||||
store.ScanDB.SetMaxOpenConns(1)
|
||||
|
||||
if err = store.CreateTable(context.Background(), abstract_sql.DEFAULT_TABLE); err != nil {
|
||||
return fmt.Errorf("init table %s: %v", abstract_sql.DEFAULT_TABLE, err)
|
||||
|
||||
@@ -0,0 +1,288 @@
|
||||
//go:build (linux || darwin || windows) && sqlite
|
||||
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer"
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
)
|
||||
|
||||
// A rename committed while a scan is running is seen under exactly one of its
|
||||
// names. The scan reads from one snapshot, so unlike a directory walk it can
|
||||
// neither miss the entry when it moves into a directory already listed, nor
|
||||
// count it twice when it moves into one not listed yet.
|
||||
func TestScanEntriesSeesConcurrentRenameExactlyOnce(t *testing.T) {
|
||||
dbFile := filepath.Join(t.TempDir(), "filer.db")
|
||||
reader := &SqliteStore{}
|
||||
if err := reader.initialize(dbFile, createTableSql, upsertQuerySql); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer reader.Shutdown()
|
||||
// WAL lets the writer commit while the scan's read transaction is open.
|
||||
if _, err := reader.DB.Exec("PRAGMA journal_mode=WAL"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writer := &SqliteStore{}
|
||||
if err := writer.initialize(dbFile, createTableSql, upsertQuerySql); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer writer.Shutdown()
|
||||
|
||||
ctx := context.Background()
|
||||
mk := func(p string) *filer.Entry {
|
||||
return &filer.Entry{FullPath: util.FullPath(p), Attr: filer.Attr{Mode: 0644, Mtime: time.Now(), Crtime: time.Now()}}
|
||||
}
|
||||
for _, p := range []string{"/ckpt/a", "/ckpt/b", "/ckpt/tmp/x"} {
|
||||
if err := reader.InsertEntry(ctx, mk(p)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
defer func(n int) { abstract_sql.SetScanPageSizeForTest(n) }(abstract_sql.SetScanPageSizeForTest(2))
|
||||
|
||||
seen := map[string]int{}
|
||||
renamed := false
|
||||
err := reader.ScanEntries(ctx, "/", func(_ context.Context, e *filer.Entry) error {
|
||||
seen[string(e.FullPath)]++
|
||||
if renamed {
|
||||
return nil
|
||||
}
|
||||
renamed = true
|
||||
// /ckpt/tmp/x -> /ckpt/x, committed by another connection mid-scan.
|
||||
if err := writer.InsertEntry(ctx, mk("/ckpt/x")); err != nil {
|
||||
return err
|
||||
}
|
||||
return writer.DeleteEntry(ctx, util.FullPath("/ckpt/tmp/x"))
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !renamed {
|
||||
t.Fatal("scan yielded nothing")
|
||||
}
|
||||
if seen["/ckpt/a"] != 1 || seen["/ckpt/b"] != 1 {
|
||||
t.Errorf("untouched entries seen %d and %d times, want once each", seen["/ckpt/a"], seen["/ckpt/b"])
|
||||
}
|
||||
if old, new := seen["/ckpt/tmp/x"], seen["/ckpt/x"]; old+new != 1 {
|
||||
t.Errorf("renamed entry seen %d time(s) under the old name and %d under the new, want exactly one in total", old, new)
|
||||
} else if old != 1 {
|
||||
t.Errorf("the rename committed after the snapshot was taken, so the scan must show the old name")
|
||||
}
|
||||
if _, err := reader.FindEntry(ctx, util.FullPath("/ckpt/x")); err != nil {
|
||||
t.Errorf("once the scan is over the new name must be visible: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Rows that are not entries, key-value records and a regular file under
|
||||
// /buckets with a bucket-like name, are stepped over; a scan rooted at a bucket
|
||||
// yields the bucket's own row (default table) and its contents (bucket table).
|
||||
func TestScanEntriesRowsAndBucketTables(t *testing.T) {
|
||||
store := &SqliteStore{}
|
||||
if err := store.initialize(filepath.Join(t.TempDir(), "filer.db"), createTableSql, upsertQuerySql); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Shutdown()
|
||||
if _, err := store.DB.Exec("PRAGMA journal_mode=WAL"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
now := time.Now()
|
||||
// The filer creates bucket tables on bucket creation; the store layer alone does not.
|
||||
if err := store.CreateTable(ctx, "realbucket"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, e := range []*filer.Entry{
|
||||
{FullPath: "/buckets", Attr: filer.Attr{Mode: 0755 | os.ModeDir, Mtime: now, Crtime: now}},
|
||||
{FullPath: "/buckets/realbucket", Attr: filer.Attr{Mode: 0755 | os.ModeDir, Mtime: now, Crtime: now}},
|
||||
{FullPath: "/buckets/realbucket/f1", Attr: filer.Attr{Mode: 0644, Mtime: now, Crtime: now}},
|
||||
{FullPath: "/buckets/readme", Attr: filer.Attr{Mode: 0644, Mtime: now, Crtime: now}},
|
||||
{FullPath: "/topics/t1", Attr: filer.Attr{Mode: 0644, Mtime: now, Crtime: now}},
|
||||
} {
|
||||
if err := store.InsertEntry(ctx, e); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, key := range []string{"filer.store.id", "/looks/like/a/path", "x"} {
|
||||
if err := store.KvPut(ctx, []byte(key), []byte("opaque value, not an Entry")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
collect := func(root string) []string {
|
||||
var got []string
|
||||
if err := store.ScanEntries(ctx, util.FullPath(root), func(_ context.Context, e *filer.Entry) error {
|
||||
got = append(got, string(e.FullPath))
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatalf("scan %s: %v", root, err)
|
||||
}
|
||||
sort.Strings(got)
|
||||
return got
|
||||
}
|
||||
if got, want := collect("/"), []string{"/buckets", "/buckets/readme", "/buckets/realbucket", "/buckets/realbucket/f1", "/topics/t1"}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("scan /: got %v, want %v", got, want)
|
||||
}
|
||||
if got, want := collect("/buckets/realbucket"), []string{"/buckets/realbucket", "/buckets/realbucket/f1"}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("scan /buckets/realbucket: got %v, want %v", got, want)
|
||||
}
|
||||
|
||||
// A bucket directory whose table does not exist fails the scan: from the
|
||||
// snapshot it cannot be told from a table dropped a moment ago.
|
||||
if err := store.InsertEntry(ctx, &filer.Entry{FullPath: "/buckets/ghostbucket", Attr: filer.Attr{Mode: 0755 | os.ModeDir, Mtime: now, Crtime: now}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err := store.ScanEntries(ctx, "/", func(context.Context, *filer.Entry) error { return nil })
|
||||
if err == nil || !strings.Contains(err.Error(), "ghostbucket") {
|
||||
t.Fatalf("scan over a bucket without a table must fail naming it, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Without WAL the store declines the scan rather than block writers for its
|
||||
// whole duration.
|
||||
func TestScanEntriesDeclinedWithoutWAL(t *testing.T) {
|
||||
store := &SqliteStore{}
|
||||
if err := store.initialize(filepath.Join(t.TempDir(), "filer.db"), createTableSql, upsertQuerySql); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Shutdown()
|
||||
err := store.ScanEntries(context.Background(), "/", func(context.Context, *filer.Entry) error { return nil })
|
||||
if !errors.Is(err, filer.ErrUnsupportedEntryScan) {
|
||||
t.Fatalf("err = %v, want ErrUnsupportedEntryScan", err)
|
||||
}
|
||||
}
|
||||
|
||||
// A store whose pool is one connection, and no scan connection of its own,
|
||||
// declines rather than hold that connection for the whole scan.
|
||||
func TestScanEntriesDeclinedOnSingleConnectionPool(t *testing.T) {
|
||||
store := &SqliteStore{}
|
||||
if err := store.initialize(filepath.Join(t.TempDir(), "filer.db"), createTableSql, upsertQuerySql); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Shutdown()
|
||||
if _, err := store.DB.Exec("PRAGMA journal_mode=WAL"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store.ScanDB.Close()
|
||||
store.ScanDB = nil
|
||||
err := store.ScanEntries(context.Background(), "/", func(context.Context, *filer.Entry) error { return nil })
|
||||
if !errors.Is(err, filer.ErrUnsupportedEntryScan) {
|
||||
t.Fatalf("err = %v, want ErrUnsupportedEntryScan", err)
|
||||
}
|
||||
}
|
||||
|
||||
// A hard-linked entry's chunks live in the shared key-value record, not in
|
||||
// its path row; the wrapper resolves them from inside the snapshot, on the
|
||||
// single connection the scan holds.
|
||||
func TestScanEntriesResolvesHardLinks(t *testing.T) {
|
||||
store := &SqliteStore{}
|
||||
if err := store.initialize(filepath.Join(t.TempDir(), "filer.db"), createTableSql, upsertQuerySql); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Shutdown()
|
||||
if _, err := store.DB.Exec("PRAGMA journal_mode=WAL"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
wrapper := filer.NewFilerStoreWrapper(store)
|
||||
ctx := context.Background()
|
||||
now := time.Now()
|
||||
linked := &filer.Entry{
|
||||
FullPath: "/buckets/hlbucket/f0001",
|
||||
Attr: filer.Attr{Mode: 0644, Mtime: now, Crtime: now},
|
||||
HardLinkId: filer.HardLinkId("hardlink-00000001"),
|
||||
HardLinkCounter: 2,
|
||||
Chunks: []*filer_pb.FileChunk{{FileId: "3,01637037d6", Size: 4096}},
|
||||
}
|
||||
if err := wrapper.InsertEntry(ctx, linked); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var got *filer.Entry
|
||||
if err := wrapper.ScanEntries(ctx, "/", func(_ context.Context, e *filer.Entry) error {
|
||||
if e.FullPath == linked.FullPath {
|
||||
got = e
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got == nil || len(got.GetChunks()) != 1 || got.GetChunks()[0].FileId != "3,01637037d6" || got.HardLinkCounter != 2 {
|
||||
t.Fatalf("hard-linked entry not resolved from its key-value record: %+v", got)
|
||||
}
|
||||
|
||||
// Once the record is gone the path row is all there is; the scan hands it
|
||||
// over instead of failing on that entry every time it runs.
|
||||
if err := store.KvDelete(ctx, []byte(linked.HardLinkId)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got = nil
|
||||
if err := wrapper.ScanEntries(ctx, "/", func(_ context.Context, e *filer.Entry) error {
|
||||
if e.FullPath == linked.FullPath {
|
||||
got = e
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatalf("a missing hard-link record must not fail the scan: %v", err)
|
||||
}
|
||||
if got == nil {
|
||||
t.Fatal("entry with a missing hard-link record not handed over")
|
||||
}
|
||||
}
|
||||
|
||||
// The keyset page must be a primary-key seek; an OR-shaped cursor predicate
|
||||
// makes SQLite scan the table from the start for every page.
|
||||
func TestScanPageQuerySeeksPrimaryKey(t *testing.T) {
|
||||
store := &SqliteStore{}
|
||||
if err := store.initialize(filepath.Join(t.TempDir(), "filer.db"), createTableSql, upsertQuerySql); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Shutdown()
|
||||
sqlText, args := store.GetSqlScanPage("filemeta", int64(-1), "", 10)
|
||||
rows, err := store.DB.Query("EXPLAIN QUERY PLAN "+sqlText, args...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var plan []string
|
||||
for rows.Next() {
|
||||
var id, parent, notused int
|
||||
var detail string
|
||||
if err := rows.Scan(&id, &parent, ¬used, &detail); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
plan = append(plan, detail)
|
||||
}
|
||||
joined := strings.Join(plan, " | ")
|
||||
if !strings.Contains(joined, "SEARCH") || strings.Contains(joined, "SCAN") {
|
||||
t.Fatalf("page query does not seek the primary key: %s", joined)
|
||||
}
|
||||
}
|
||||
|
||||
// A bucket name only the process cache remembers may have its table dropped
|
||||
// meanwhile; the scan skips it instead of failing.
|
||||
func TestScanEntriesSkipsCachedOnlyBucketTable(t *testing.T) {
|
||||
store := &SqliteStore{}
|
||||
if err := store.initialize(filepath.Join(t.TempDir(), "filer.db"), createTableSql, upsertQuerySql); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Shutdown()
|
||||
if _, err := store.DB.Exec("PRAGMA journal_mode=WAL"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store.OnBucketCreation("gonebucket") // the table is never created
|
||||
if err := store.ScanEntries(context.Background(), "/", func(context.Context, *filer.Entry) error { return nil }); err != nil {
|
||||
t.Fatalf("cached-only bucket with a dropped table must not fail the scan: %v", err)
|
||||
}
|
||||
}
|
||||
+17
-12
@@ -98,21 +98,26 @@ var fineGrainedActionMap = map[string]string{
|
||||
// prefixed (s3:Action) and unprefixed (Action) variants in init().
|
||||
var baseS3ActionMap = map[string]string{
|
||||
// Object operations
|
||||
"DeleteObject": s3_constants.ACTION_WRITE,
|
||||
"PutObject": s3_constants.ACTION_WRITE,
|
||||
"GetObject": s3_constants.ACTION_READ,
|
||||
"DeleteObjectVersion": s3_constants.ACTION_WRITE,
|
||||
"GetObjectVersion": s3_constants.ACTION_READ,
|
||||
"DeleteObject": s3_constants.ACTION_WRITE,
|
||||
"PutObject": s3_constants.ACTION_WRITE,
|
||||
"GetObject": s3_constants.ACTION_READ,
|
||||
"DeleteObjectVersion": s3_constants.ACTION_WRITE,
|
||||
"GetObjectVersion": s3_constants.ACTION_READ,
|
||||
"GetObjectAttributes": s3_constants.ACTION_READ,
|
||||
"GetObjectVersionAttributes": s3_constants.ACTION_READ,
|
||||
// Tagging operations
|
||||
"GetObjectTagging": s3_constants.ACTION_TAGGING,
|
||||
"GetObjectVersionTagging": s3_constants.ACTION_TAGGING,
|
||||
"PutObjectTagging": s3_constants.ACTION_TAGGING,
|
||||
"DeleteObjectTagging": s3_constants.ACTION_TAGGING,
|
||||
"GetBucketTagging": s3_constants.ACTION_TAGGING,
|
||||
"PutBucketTagging": s3_constants.ACTION_TAGGING,
|
||||
"DeleteBucketTagging": s3_constants.ACTION_TAGGING,
|
||||
"GetObjectTagging": s3_constants.ACTION_TAGGING,
|
||||
"GetObjectVersionTagging": s3_constants.ACTION_TAGGING,
|
||||
"PutObjectTagging": s3_constants.ACTION_TAGGING,
|
||||
"PutObjectVersionTagging": s3_constants.ACTION_TAGGING,
|
||||
"DeleteObjectTagging": s3_constants.ACTION_TAGGING,
|
||||
"DeleteObjectVersionTagging": s3_constants.ACTION_TAGGING,
|
||||
"GetBucketTagging": s3_constants.ACTION_TAGGING,
|
||||
"PutBucketTagging": s3_constants.ACTION_TAGGING,
|
||||
"DeleteBucketTagging": s3_constants.ACTION_TAGGING,
|
||||
// ACL operations
|
||||
"PutObjectAcl": s3_constants.ACTION_WRITE_ACP,
|
||||
"PutObjectVersionAcl": s3_constants.ACTION_WRITE_ACP,
|
||||
"GetObjectAcl": s3_constants.ACTION_READ_ACP,
|
||||
"GetObjectVersionAcl": s3_constants.ACTION_READ_ACP,
|
||||
"PutBucketAcl": s3_constants.ACTION_WRITE_ACP,
|
||||
|
||||
@@ -100,6 +100,11 @@ type STSSessionClaims struct {
|
||||
// guaranteed stable per OpenID Connect Core 1.0. Empty for non-federated
|
||||
// session types.
|
||||
ParentUser string `json:"puid,omitempty"`
|
||||
|
||||
// SourceAccessKeyId is the access key the caller authenticated with when
|
||||
// a SeaweedFS service minted the session for it (AssumeRoleForPrincipal);
|
||||
// empty for every other session type.
|
||||
SourceAccessKeyId string `json:"src_ak,omitempty"`
|
||||
}
|
||||
|
||||
// NewSTSSessionClaims creates new STS session claims with all required information
|
||||
@@ -156,6 +161,8 @@ func (c *STSSessionClaims) ToSessionInfo(credGen *CredentialGenerator) *SessionI
|
||||
ProviderIssuer: c.ProviderIssuer,
|
||||
RequestContext: c.RequestContext,
|
||||
ParentUser: c.ParentUser,
|
||||
// The access key the session was minted for, if recorded
|
||||
SourceAccessKeyId: c.SourceAccessKeyId,
|
||||
// Provide the Subject (sub) from registered claims
|
||||
Subject: c.Subject,
|
||||
Credentials: credentials,
|
||||
@@ -250,6 +257,12 @@ func (c *STSSessionClaims) WithSessionName(sessionName string) *STSSessionClaims
|
||||
return c
|
||||
}
|
||||
|
||||
// WithSourceAccessKeyId records the access key the session was minted for.
|
||||
func (c *STSSessionClaims) WithSourceAccessKeyId(accessKeyId string) *STSSessionClaims {
|
||||
c.SourceAccessKeyId = accessKeyId
|
||||
return c
|
||||
}
|
||||
|
||||
// WithParentUser sets the stable per-identity hash for the session. See
|
||||
// ComputeParentUser for the derivation rule.
|
||||
func (c *STSSessionClaims) WithParentUser(parentUser string) *STSSessionClaims {
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package sts
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func newSourceKeyTestSTS(t *testing.T) *STSService {
|
||||
t.Helper()
|
||||
s := NewSTSService()
|
||||
require.NoError(t, s.Initialize(&STSConfig{
|
||||
TokenDuration: FlexibleDuration{Duration: time.Hour},
|
||||
MaxSessionLength: FlexibleDuration{Duration: 12 * time.Hour},
|
||||
Issuer: "test-issuer",
|
||||
SigningKey: []byte("test-signing-key-at-least-32-bytes-long-for-security"),
|
||||
}))
|
||||
return s
|
||||
}
|
||||
|
||||
// A session minted for a caller's access key carries that key back out of its
|
||||
// token; a session minted without one carries none.
|
||||
func TestSessionCarriesItsSourceAccessKey(t *testing.T) {
|
||||
s := newSourceKeyTestSTS(t)
|
||||
resp, err := s.issueSession("arn:aws:iam::role/IcebergTableAccess", "", "iceberg-alice-ns-t-kabc", "", nil,
|
||||
"iceberg-catalog", "alice", func(c *STSSessionClaims) { c.WithSourceAccessKeyId("AKIASOURCE") })
|
||||
require.NoError(t, err)
|
||||
info, err := s.ValidateSessionToken(context.Background(), resp.Credentials.SessionToken)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "AKIASOURCE", info.SourceAccessKeyId)
|
||||
assert.Equal(t, "alice", info.ExternalUserId)
|
||||
assert.Equal(t, "iceberg-catalog", info.IdentityProvider)
|
||||
|
||||
resp, err = s.issueSession("arn:aws:iam::role/IcebergTableAccess", "", "iceberg-alice-ns-t", "", nil, "iceberg-catalog", "alice")
|
||||
require.NoError(t, err)
|
||||
info, err = s.ValidateSessionToken(context.Background(), resp.Credentials.SessionToken)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "", info.SourceAccessKeyId)
|
||||
}
|
||||
@@ -316,6 +316,10 @@ type SessionInfo struct {
|
||||
// RoleId is the unique ID of the role the session was issued for; empty
|
||||
// for sessions issued before role IDs were recorded.
|
||||
RoleId string `json:"roleId,omitempty"`
|
||||
|
||||
// SourceAccessKeyId is the access key the session was minted for, when the
|
||||
// minting service recorded it (AssumeRoleForPrincipal).
|
||||
SourceAccessKeyId string `json:"sourceAccessKeyId,omitempty"`
|
||||
}
|
||||
|
||||
// NewSTSService creates a new STS service
|
||||
@@ -780,7 +784,7 @@ func (s *STSService) AssumeRoleWithCredentials(ctx context.Context, request *Ass
|
||||
// issueSession mints temporary credentials and the self-contained JWT that
|
||||
// carries the whole session, shared by every assume-role entry point.
|
||||
func (s *STSService) issueSession(roleArn, roleId, roleSessionName, sessionPolicy string,
|
||||
durationSeconds *int64, providerName, subject string) (*AssumeRoleResponse, error) {
|
||||
durationSeconds *int64, providerName, subject string, options ...func(*STSSessionClaims)) (*AssumeRoleResponse, error) {
|
||||
|
||||
sessionDuration := s.CalculateSessionDuration(durationSeconds)
|
||||
expiresAt := time.Now().Add(sessionDuration)
|
||||
@@ -810,6 +814,9 @@ func (s *STSService) issueSession(roleArn, roleId, roleSessionName, sessionPolic
|
||||
if sessionPolicy != "" {
|
||||
sessionClaims.WithSessionPolicy(sessionPolicy)
|
||||
}
|
||||
for _, option := range options {
|
||||
option(sessionClaims)
|
||||
}
|
||||
|
||||
jwtToken, err := s.tokenGenerator.GenerateJWTWithClaims(sessionClaims)
|
||||
if err != nil {
|
||||
@@ -863,6 +870,10 @@ type AssumeRoleForPrincipalRequest struct {
|
||||
|
||||
// DurationSeconds requests a session lifetime.
|
||||
DurationSeconds *int64
|
||||
|
||||
// SourceAccessKeyId optionally records the access key the caller
|
||||
// authenticated with, so the session can be tied to that key.
|
||||
SourceAccessKeyId string
|
||||
}
|
||||
|
||||
// AssumeRoleForPrincipal issues session credentials for a caller that a
|
||||
@@ -908,7 +919,8 @@ func (s *STSService) AssumeRoleForPrincipal(ctx context.Context, request *Assume
|
||||
// Iceberg credential vending resolves no role definition, so its sessions
|
||||
// carry no role ID and stay bound by name.
|
||||
return s.issueSession(request.RoleArn, "", request.RoleSessionName, sessionPolicy,
|
||||
request.DurationSeconds, request.ProviderName, request.Principal)
|
||||
request.DurationSeconds, request.ProviderName, request.Principal,
|
||||
func(c *STSSessionClaims) { c.WithSourceAccessKeyId(request.SourceAccessKeyId) })
|
||||
}
|
||||
|
||||
// ValidateSessionToken validates a session token and returns session information
|
||||
|
||||
@@ -2,10 +2,16 @@ package iamapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -141,3 +147,69 @@ func TestIamManagementAllowedForAdmin(t *testing.T) {
|
||||
"", "Action=CreateUser&UserName=new-user&Version=2010-05-08")
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
}
|
||||
|
||||
// The implicit UserName must come from the identity authentication verified,
|
||||
// never from re-parsing the Authorization header: a request can carry presigned
|
||||
// credentials for one user alongside a junk SigV4 header naming another.
|
||||
func TestImplicitUserNameUsesVerifiedIdentity(t *testing.T) {
|
||||
server := newAuthzTestServer(t)
|
||||
expires := strconv.FormatInt(time.Now().Add(10*time.Minute).Unix(), 10)
|
||||
sts := "POST\n\napplication/x-www-form-urlencoded\n" + expires + "\n/"
|
||||
mac := hmac.New(sha1.New, []byte("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"))
|
||||
mac.Write([]byte(sts))
|
||||
sig := base64.StdEncoding.EncodeToString(mac.Sum(nil))
|
||||
q := url.Values{"AWSAccessKeyId": {"AKIAIOSFODNN7EXAMPLE"}, "Expires": {expires}, "Signature": {sig}}
|
||||
|
||||
post := func(t *testing.T, body string) (int, string) {
|
||||
t.Helper()
|
||||
req, err := http.NewRequest(http.MethodPost, server.URL+"/?"+q.Encode(), strings.NewReader(body))
|
||||
require.NoError(t, err)
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
req.Header.Set("Authorization", "AWS4-HMAC-SHA256 Credential=AKIATESTFAKEADMIN0001/20261021/us-east-1/iam/aws4_request, SignedHeaders=host, Signature=00")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
b, err := io.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
t.Logf("%d %s", resp.StatusCode, b)
|
||||
return resp.StatusCode, string(b)
|
||||
}
|
||||
|
||||
for _, action := range []string{"ListAccessKeys", "CreateAccessKey", "GetUser"} {
|
||||
t.Run(action, func(t *testing.T) {
|
||||
code, body := post(t, "Action="+action+"&Version=2010-05-08")
|
||||
assert.Equal(t, http.StatusOK, code)
|
||||
assert.NotContains(t, body, "iam_admin")
|
||||
assert.Contains(t, body, "power_user")
|
||||
})
|
||||
}
|
||||
|
||||
// ListUserPolicies is not self-service: a caller without IAM rights is
|
||||
// denied whichever name the implicit resolution lands on.
|
||||
t.Run("ListUserPolicies", func(t *testing.T) {
|
||||
code, body := post(t, "Action=ListUserPolicies&Version=2010-05-08")
|
||||
assert.Equal(t, http.StatusForbidden, code)
|
||||
assert.NotContains(t, body, "iam_admin")
|
||||
})
|
||||
|
||||
// A write aimed at the header-named admin's key resolves to the verified
|
||||
// caller instead: the key is not power_user's, so it is not found.
|
||||
t.Run("UpdateAccessKey", func(t *testing.T) {
|
||||
code, body := post(t, "Action=UpdateAccessKey&AccessKeyId=AKIATESTFAKEADMIN0001&Status=Inactive&Version=2010-05-08")
|
||||
assert.Equal(t, http.StatusNotFound, code)
|
||||
assert.Contains(t, body, "NoSuchEntity")
|
||||
})
|
||||
|
||||
t.Run("DeleteAccessKey", func(t *testing.T) {
|
||||
code, _ := post(t, "Action=DeleteAccessKey&AccessKeyId=AKIATESTFAKEADMIN0001&Version=2010-05-08")
|
||||
assert.Equal(t, http.StatusOK, code)
|
||||
})
|
||||
|
||||
// The admin key the junk header named is untouched: still present and active.
|
||||
resp := postSignedIamAction(t, server.URL, "AKIATESTFAKEADMIN0001", "testAdminSecretFake0000000000000000000000",
|
||||
"", "Action=ListAccessKeys&Version=2010-05-08")
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, string(body), "AKIATESTFAKEADMIN0001")
|
||||
assert.Contains(t, string(body), "<Status>Active</Status>")
|
||||
}
|
||||
@@ -4,6 +4,7 @@ package iamapi
|
||||
// Common IAM types and helpers are imported from the shared weed/iam package.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -14,12 +15,14 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/iam"
|
||||
"github.com/seaweedfs/seaweedfs/weed/credential"
|
||||
"github.com/seaweedfs/seaweedfs/weed/glog"
|
||||
iamlib "github.com/seaweedfs/seaweedfs/weed/iam"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api"
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/policy_engine"
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util/request_id"
|
||||
)
|
||||
@@ -1263,53 +1266,128 @@ func (iama *IamApiServer) DeleteAccessKey(s3cfg *iam_pb.S3ApiConfiguration, valu
|
||||
return resp
|
||||
}
|
||||
|
||||
// handleImplicitUsername adds username who signs the request to values if 'username' is not specified.
|
||||
// According to AWS documentation: "If you do not specify a user name, IAM determines the user name
|
||||
// implicitly based on the Amazon Web Services access key ID signing the request."
|
||||
// This function extracts the AccessKeyId from the SigV4 credential and looks up the corresponding
|
||||
// identity name in the credential store.
|
||||
// handleImplicitUsername adds the name of the authenticated caller to values
|
||||
// when 'UserName' is not specified, per AWS: "If you do not specify a user name,
|
||||
// IAM determines the user name implicitly based on the Amazon Web Services
|
||||
// access key ID signing the request." The name comes from the identity the
|
||||
// authentication middleware verified, which the request context carries; the
|
||||
// Authorization header itself is attacker-controlled and is never re-parsed.
|
||||
func (iama *IamApiServer) handleImplicitUsername(r *http.Request, values url.Values) {
|
||||
if len(r.Header["Authorization"]) == 0 || values.Get("UserName") != "" {
|
||||
if values.Get("UserName") != "" {
|
||||
return
|
||||
}
|
||||
// Log presence of auth header without exposing sensitive signature material
|
||||
glog.V(4).Infof("Authorization header present, extracting access key")
|
||||
// Parse AWS SigV4 Authorization header format:
|
||||
// "AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20130524/us-east-1/iam/aws4_request, ..."
|
||||
s := strings.Split(r.Header["Authorization"][0], "Credential=")
|
||||
if len(s) < 2 {
|
||||
return
|
||||
if name := s3_constants.GetIdentityNameFromContext(r); name != "" {
|
||||
values.Set("UserName", name)
|
||||
}
|
||||
s = strings.Split(s[1], ",")
|
||||
if len(s) < 1 {
|
||||
return
|
||||
}
|
||||
s = strings.Split(s[0], "/")
|
||||
if len(s) < 1 {
|
||||
return
|
||||
}
|
||||
// s[0] is the AccessKeyId
|
||||
accessKeyId := s[0]
|
||||
if accessKeyId == "" {
|
||||
return
|
||||
}
|
||||
// Nil-guard: ensure iam is initialized before lookup
|
||||
}
|
||||
|
||||
// targetedCredentialManager returns the credential manager through which a change to the named
|
||||
// identity is written on its own instead of via SaveConfiguration, or nil when this server has
|
||||
// none, or the identity is static: static identities live outside the store and keep the
|
||||
// full-save path.
|
||||
func (iama *IamApiServer) targetedCredentialManager(userName string) *credential.CredentialManager {
|
||||
if iama.iam == nil {
|
||||
glog.V(4).Infof("IAM not initialized, cannot look up access key")
|
||||
return
|
||||
return nil
|
||||
}
|
||||
// Look up the identity by access key to get the username
|
||||
identity, _, found := iama.iam.LookupByAccessKey(accessKeyId)
|
||||
if !found {
|
||||
// Mask access key in logs - show only first 4 chars
|
||||
maskedKey := accessKeyId
|
||||
if len(accessKeyId) > 4 {
|
||||
maskedKey = accessKeyId[:4] + "***"
|
||||
cm := iama.iam.GetCredentialManager()
|
||||
if cm == nil || cm.IsStaticIdentity(userName) {
|
||||
return nil
|
||||
}
|
||||
return cm
|
||||
}
|
||||
|
||||
// findIdentityByName returns the identity named userName in s3cfg, or nil.
|
||||
func findIdentityByName(s3cfg *iam_pb.S3ApiConfiguration, userName string) *iam_pb.Identity {
|
||||
for _, ident := range s3cfg.Identities {
|
||||
if ident.Name == userName {
|
||||
return ident
|
||||
}
|
||||
glog.V(4).Infof("Access key %s not found in credential store", maskedKey)
|
||||
return
|
||||
}
|
||||
values.Set("UserName", identity.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
// groupsWithMember returns the groups in s3cfg that list userName as a member. Taken before a
|
||||
// handler renames or removes the user, it names the group records that handler will change.
|
||||
func groupsWithMember(s3cfg *iam_pb.S3ApiConfiguration, userName string) []*iam_pb.Group {
|
||||
var groups []*iam_pb.Group
|
||||
for _, g := range s3cfg.Groups {
|
||||
for _, m := range g.Members {
|
||||
if m == userName {
|
||||
groups = append(groups, g)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return groups
|
||||
}
|
||||
|
||||
// serviceAccountsOf returns the service accounts in s3cfg whose parent is userName.
|
||||
func serviceAccountsOf(s3cfg *iam_pb.S3ApiConfiguration, userName string) []*iam_pb.ServiceAccount {
|
||||
var accounts []*iam_pb.ServiceAccount
|
||||
for _, sa := range s3cfg.ServiceAccounts {
|
||||
if sa.ParentUser == userName {
|
||||
accounts = append(accounts, sa)
|
||||
}
|
||||
}
|
||||
return accounts
|
||||
}
|
||||
|
||||
// persistIdentity writes the one identity a handler changed in s3cfg through the credential
|
||||
// manager. The store writes that identity's record and propagates it alone; the other
|
||||
// identities are not rewritten.
|
||||
func persistIdentity(ctx context.Context, cm *credential.CredentialManager, s3cfg *iam_pb.S3ApiConfiguration, userName string) error {
|
||||
ident := findIdentityByName(s3cfg, userName)
|
||||
if ident == nil {
|
||||
return credential.ErrUserNotFound
|
||||
}
|
||||
return cm.UpdateUser(ctx, userName, ident)
|
||||
}
|
||||
|
||||
// persistRenamedIdentity writes a user renamed from oldName to newName by UpdateUser through the
|
||||
// credential manager: the identity, the groups that listed the old name and the service accounts
|
||||
// it parented. UpdateUser under the old name moves the record in stores that treat a changed Name
|
||||
// as a rename (filer_etc, memory), and the propagating store propagates the new identity and the
|
||||
// old name's removal; UpdateUser already moved the inline policies through PutPolicies. A store
|
||||
// that did not move the record still holds the user under the old name; the caller is told so
|
||||
// (false) and keeps the full save, which is what renames such a store today.
|
||||
func persistRenamedIdentity(ctx context.Context, cm *credential.CredentialManager, s3cfg *iam_pb.S3ApiConfiguration, oldName, newName string, groups []*iam_pb.Group, accounts []*iam_pb.ServiceAccount) (bool, error) {
|
||||
ident := findIdentityByName(s3cfg, newName)
|
||||
if ident == nil {
|
||||
return false, credential.ErrUserNotFound
|
||||
}
|
||||
if err := cm.UpdateUser(ctx, oldName, ident); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err := cm.GetUser(ctx, newName); err != nil {
|
||||
return false, nil
|
||||
}
|
||||
for _, g := range groups {
|
||||
if err := cm.UpdateGroup(ctx, g); err != nil {
|
||||
return false, fmt.Errorf("update group %s: %w", g.Name, err)
|
||||
}
|
||||
}
|
||||
for _, sa := range accounts {
|
||||
if err := cm.UpdateServiceAccount(ctx, sa.Id, sa); err != nil {
|
||||
return false, fmt.Errorf("update service account %s: %w", sa.Id, err)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// deleteIdentity removes a user deleted by DeleteUser through the credential manager: its record
|
||||
// (the store propagates the one removal) and the groups that listed it, whose membership
|
||||
// DeleteUser already changed in s3cfg. DeleteUser already removed the inline policies through
|
||||
// PutPolicies.
|
||||
func deleteIdentity(ctx context.Context, cm *credential.CredentialManager, userName string, groups []*iam_pb.Group) error {
|
||||
if err := cm.DeleteUser(ctx, userName); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, g := range groups {
|
||||
if err := cm.UpdateGroup(ctx, g); err != nil {
|
||||
return fmt.Errorf("update group %s: %w", g.Name, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -1333,6 +1411,9 @@ func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
|
||||
glog.V(4).Infof("DoActions: %+v", iamlib.RedactSensitiveFormValues(values))
|
||||
var response iamlib.RequestIDSetter
|
||||
changed := true
|
||||
// persisted is set by the actions that wrote one identity through the credential manager
|
||||
// instead of SaveConfiguration; the in-memory identity maps are reloaded after them too.
|
||||
persisted := false
|
||||
switch r.Form.Get("Action") {
|
||||
case "ListUsers":
|
||||
response = iama.ListUsers(s3cfg, values)
|
||||
@@ -1356,6 +1437,7 @@ func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
case "GetUser":
|
||||
iama.handleImplicitUsername(r, values)
|
||||
userName := values.Get("UserName")
|
||||
var err *IamError
|
||||
response, err = iama.GetUser(s3cfg, userName)
|
||||
@@ -1365,40 +1447,107 @@ func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
changed = false
|
||||
case "UpdateUser":
|
||||
userName, newUserName := values.Get("UserName"), values.Get("NewUserName")
|
||||
// Taken before the rename: the records the handler changes besides the identity.
|
||||
groups := groupsWithMember(s3cfg, userName)
|
||||
accounts := serviceAccountsOf(s3cfg, userName)
|
||||
var err *IamError
|
||||
response, err = iama.UpdateUser(s3cfg, values)
|
||||
if err != nil {
|
||||
writeIamErrorResponse(w, r, reqID, err)
|
||||
return
|
||||
}
|
||||
// Write the renamed identity, its groups and service accounts through the credential
|
||||
// manager instead of rewriting every identity via SaveConfiguration. A request that
|
||||
// renames nothing changed nothing, so there is nothing to write either way.
|
||||
if cm := iama.targetedCredentialManager(userName); cm != nil {
|
||||
renamed := true
|
||||
if newUserName != "" && newUserName != userName {
|
||||
var err error
|
||||
renamed, err = persistRenamedIdentity(r.Context(), cm, s3cfg, userName, newUserName, groups, accounts)
|
||||
if err != nil {
|
||||
writeIamErrorResponse(w, r, reqID, &IamError{Code: s3api.CredentialErrToIamErrCode(err), Error: err})
|
||||
return
|
||||
}
|
||||
}
|
||||
changed, persisted = !renamed, renamed
|
||||
}
|
||||
case "DeleteUser":
|
||||
userName := values.Get("UserName")
|
||||
groups := groupsWithMember(s3cfg, userName)
|
||||
var err *IamError
|
||||
response, err = iama.DeleteUser(s3cfg, userName)
|
||||
if err != nil {
|
||||
writeIamErrorResponse(w, r, reqID, err)
|
||||
return
|
||||
}
|
||||
// Targeted delete: one record removed and one removal propagated, plus the groups whose
|
||||
// membership changed, instead of rewriting every remaining identity.
|
||||
if cm := iama.targetedCredentialManager(userName); cm != nil {
|
||||
if err := deleteIdentity(r.Context(), cm, userName, groups); err != nil {
|
||||
writeIamErrorResponse(w, r, reqID, &IamError{Code: s3api.CredentialErrToIamErrCode(err), Error: err})
|
||||
return
|
||||
}
|
||||
changed, persisted = false, true
|
||||
}
|
||||
case "CreateAccessKey":
|
||||
iama.handleImplicitUsername(r, values)
|
||||
var err *IamError
|
||||
response, err = iama.CreateAccessKey(s3cfg, values)
|
||||
userName := values.Get("UserName")
|
||||
resp, err := iama.CreateAccessKey(s3cfg, values)
|
||||
if err != nil {
|
||||
glog.Errorf("CreateAccessKey: %+v", err.Error)
|
||||
writeIamErrorResponse(w, r, reqID, err)
|
||||
return
|
||||
}
|
||||
response = resp
|
||||
// Targeted write: credentialManager.CreateAccessKey adds the key to the one identity and
|
||||
// propagates that identity alone. The handler creates the identity for a user it does
|
||||
// not know; that case still needs the full save.
|
||||
if cm := iama.targetedCredentialManager(userName); cm != nil {
|
||||
if _, known := cm.GetUser(r.Context(), userName); known == nil {
|
||||
cred := &iam_pb.Credential{
|
||||
AccessKey: *resp.CreateAccessKeyResult.AccessKey.AccessKeyId,
|
||||
SecretKey: *resp.CreateAccessKeyResult.AccessKey.SecretAccessKey,
|
||||
Status: accessKeyStatusActive,
|
||||
}
|
||||
if err := cm.CreateAccessKey(r.Context(), userName, cred); err != nil {
|
||||
writeIamErrorResponse(w, r, reqID, &IamError{Code: s3api.CredentialErrToIamErrCode(err), Error: err})
|
||||
return
|
||||
}
|
||||
changed, persisted = false, true
|
||||
}
|
||||
}
|
||||
case "DeleteAccessKey":
|
||||
iama.handleImplicitUsername(r, values)
|
||||
userName, accessKeyId := values.Get("UserName"), values.Get("AccessKeyId")
|
||||
response = iama.DeleteAccessKey(s3cfg, values)
|
||||
// Targeted write: credentialManager.DeleteAccessKey removes the key from the one identity
|
||||
// and propagates that identity alone. The handler answers 200 for a key or user it does
|
||||
// not know, so a not-found from the store is the same no-op it always was.
|
||||
if cm := iama.targetedCredentialManager(userName); cm != nil {
|
||||
err := cm.DeleteAccessKey(r.Context(), userName, accessKeyId)
|
||||
if err != nil && !errors.Is(err, credential.ErrAccessKeyNotFound) && !errors.Is(err, credential.ErrUserNotFound) {
|
||||
writeIamErrorResponse(w, r, reqID, &IamError{Code: s3api.CredentialErrToIamErrCode(err), Error: err})
|
||||
return
|
||||
}
|
||||
changed, persisted = false, true
|
||||
}
|
||||
case "UpdateAccessKey":
|
||||
iama.handleImplicitUsername(r, values)
|
||||
userName := values.Get("UserName")
|
||||
var err *IamError
|
||||
response, err = iama.UpdateAccessKey(s3cfg, values)
|
||||
if err != nil {
|
||||
writeIamErrorResponse(w, r, reqID, err)
|
||||
return
|
||||
}
|
||||
if cm := iama.targetedCredentialManager(userName); cm != nil {
|
||||
if err := persistIdentity(r.Context(), cm, s3cfg, userName); err != nil {
|
||||
writeIamErrorResponse(w, r, reqID, &IamError{Code: s3api.CredentialErrToIamErrCode(err), Error: err})
|
||||
return
|
||||
}
|
||||
changed, persisted = false, true
|
||||
}
|
||||
case "CreatePolicy":
|
||||
var err *IamError
|
||||
response, err = iama.CreatePolicy(s3cfg, values)
|
||||
@@ -1410,6 +1559,7 @@ func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
|
||||
// The `changed` flag is false because this does not modify the main s3cfg.Identities configuration.
|
||||
changed = false
|
||||
case "PutUserPolicy":
|
||||
userName := values.Get("UserName")
|
||||
var err *IamError
|
||||
response, err = iama.PutUserPolicy(s3cfg, values)
|
||||
if err != nil {
|
||||
@@ -1417,6 +1567,15 @@ func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
|
||||
writeIamErrorResponse(w, r, reqID, err)
|
||||
return
|
||||
}
|
||||
// The policy document is already stored through PutPolicies; what remains is the
|
||||
// identity's recomputed Actions, written and propagated for that identity alone.
|
||||
if cm := iama.targetedCredentialManager(userName); cm != nil {
|
||||
if err := persistIdentity(r.Context(), cm, s3cfg, userName); err != nil {
|
||||
writeIamErrorResponse(w, r, reqID, &IamError{Code: s3api.CredentialErrToIamErrCode(err), Error: err})
|
||||
return
|
||||
}
|
||||
changed, persisted = false, true
|
||||
}
|
||||
case "GetUserPolicy":
|
||||
var err *IamError
|
||||
response, err = iama.GetUserPolicy(s3cfg, values)
|
||||
@@ -1426,12 +1585,20 @@ func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
changed = false
|
||||
case "DeleteUserPolicy":
|
||||
userName := values.Get("UserName")
|
||||
var err *IamError
|
||||
response, err = iama.DeleteUserPolicy(s3cfg, values)
|
||||
if err != nil {
|
||||
writeIamErrorResponse(w, r, reqID, err)
|
||||
return
|
||||
}
|
||||
if cm := iama.targetedCredentialManager(userName); cm != nil {
|
||||
if err := persistIdentity(r.Context(), cm, s3cfg, userName); err != nil {
|
||||
writeIamErrorResponse(w, r, reqID, &IamError{Code: s3api.CredentialErrToIamErrCode(err), Error: err})
|
||||
return
|
||||
}
|
||||
changed, persisted = false, true
|
||||
}
|
||||
case "ListUserPolicies":
|
||||
iama.handleImplicitUsername(r, values)
|
||||
var err *IamError
|
||||
@@ -1642,6 +1809,8 @@ func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
|
||||
writeIamErrorResponse(w, r, reqID, &iamError)
|
||||
return
|
||||
}
|
||||
}
|
||||
if changed || persisted {
|
||||
// Reload in-memory identity maps so subsequent LookupByAccessKey calls
|
||||
// can see newly created or deleted keys immediately
|
||||
if iama.iam != nil {
|
||||
|
||||
@@ -42,10 +42,11 @@ type IamS3ApiConfigure struct {
|
||||
}
|
||||
|
||||
type IamServerOption struct {
|
||||
Masters map[string]pb.ServerAddress
|
||||
Filers []pb.ServerAddress
|
||||
Port int
|
||||
GrpcDialOption grpc.DialOption
|
||||
Masters map[string]pb.ServerAddress
|
||||
Filers []pb.ServerAddress
|
||||
Port int
|
||||
GrpcDialOption grpc.DialOption
|
||||
IamSelfServiceRequiresGrant bool
|
||||
}
|
||||
|
||||
type IamApiServer struct {
|
||||
@@ -87,8 +88,9 @@ func NewIamApiServerWithStore(router *mux.Router, option *IamServerOption, expli
|
||||
s3ApiConfigure = configure
|
||||
|
||||
s3Option := s3api.S3ApiServerOption{
|
||||
Filers: option.Filers,
|
||||
GrpcDialOption: option.GrpcDialOption,
|
||||
Filers: option.Filers,
|
||||
GrpcDialOption: option.GrpcDialOption,
|
||||
IamSelfServiceRequiresGrant: option.IamSelfServiceRequiresGrant,
|
||||
}
|
||||
|
||||
// Initialize FilerClient for IAM - explicit filers only (no discovery as FilerGroup unspecified)
|
||||
|
||||
@@ -0,0 +1,302 @@
|
||||
package iamapi
|
||||
|
||||
// Targeted-write regression tests: single-identity IAM actions must write only
|
||||
// the identity they changed through the credential manager, not every identity
|
||||
// via PutS3ApiConfiguration/SaveConfiguration.
|
||||
// See https://github.com/seaweedfs/seaweedfs/issues/11865.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/iam"
|
||||
"github.com/seaweedfs/seaweedfs/weed/credential"
|
||||
"github.com/seaweedfs/seaweedfs/weed/credential/memory"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// targetedRecordingStore wraps a memory store and counts the write methods
|
||||
// the dispatcher used, so a test can assert the single-identity path was
|
||||
// taken. It embeds the concrete store (rather than the CredentialStore
|
||||
// interface) so optional interfaces like InlinePolicyStore stay visible
|
||||
// through the wrapper.
|
||||
type targetedRecordingStore struct {
|
||||
*memory.MemoryStore
|
||||
saves int
|
||||
createAccessKeys int
|
||||
deleteAccessKeys int
|
||||
updateUsers int
|
||||
deleteUsers int
|
||||
updateGroups int
|
||||
}
|
||||
|
||||
func (r *targetedRecordingStore) SaveConfiguration(ctx context.Context, config *iam_pb.S3ApiConfiguration) error {
|
||||
r.saves++
|
||||
return r.MemoryStore.SaveConfiguration(ctx, config)
|
||||
}
|
||||
|
||||
func (r *targetedRecordingStore) CreateAccessKey(ctx context.Context, username string, cred *iam_pb.Credential) error {
|
||||
r.createAccessKeys++
|
||||
return r.MemoryStore.CreateAccessKey(ctx, username, cred)
|
||||
}
|
||||
|
||||
func (r *targetedRecordingStore) DeleteAccessKey(ctx context.Context, username string, accessKey string) error {
|
||||
r.deleteAccessKeys++
|
||||
return r.MemoryStore.DeleteAccessKey(ctx, username, accessKey)
|
||||
}
|
||||
|
||||
func (r *targetedRecordingStore) UpdateUser(ctx context.Context, username string, identity *iam_pb.Identity) error {
|
||||
r.updateUsers++
|
||||
return r.MemoryStore.UpdateUser(ctx, username, identity)
|
||||
}
|
||||
|
||||
func (r *targetedRecordingStore) DeleteUser(ctx context.Context, username string) error {
|
||||
r.deleteUsers++
|
||||
return r.MemoryStore.DeleteUser(ctx, username)
|
||||
}
|
||||
|
||||
func (r *targetedRecordingStore) UpdateGroup(ctx context.Context, group *iam_pb.Group) error {
|
||||
r.updateGroups++
|
||||
return r.MemoryStore.UpdateGroup(ctx, group)
|
||||
}
|
||||
|
||||
func targetedIamApiServer() (*IamApiServer, *credential.CredentialManager, *countingConfigSaver, *targetedRecordingStore) {
|
||||
srv, cm, counter := iamApiServerWithCredentialManager()
|
||||
rec := &targetedRecordingStore{MemoryStore: cm.Store.(*memory.MemoryStore)}
|
||||
cm.Store = rec
|
||||
return srv, cm, counter, rec
|
||||
}
|
||||
|
||||
const targetedTestPolicy = `{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [{"Effect": "Allow", "Action": ["s3:GetObject"], "Resource": ["arn:aws:s3:::bench/*"]}]
|
||||
}`
|
||||
|
||||
func TestIamApiCreateAccessKeyTargeted(t *testing.T) {
|
||||
srv, cm, counter, rec := targetedIamApiServer()
|
||||
ctx := context.Background()
|
||||
require.NoError(t, cm.CreateUser(ctx, &iam_pb.Identity{Name: "alice"}))
|
||||
require.NoError(t, cm.CreateUser(ctx, &iam_pb.Identity{Name: "bob"}))
|
||||
|
||||
req, _ := iam.New(session.New()).CreateAccessKeyRequest(&iam.CreateAccessKeyInput{UserName: aws.String("alice")})
|
||||
_ = req.Build()
|
||||
out := CreateAccessKeyResponse{}
|
||||
resp, err := executeRequestWith(srv, req.HTTPRequest, &out)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, resp.Code)
|
||||
|
||||
assert.Equal(t, 0, counter.putCalled, "CreateAccessKey must not PutS3ApiConfiguration")
|
||||
assert.Equal(t, 0, rec.saves)
|
||||
assert.Equal(t, 1, rec.createAccessKeys)
|
||||
|
||||
ident, err := cm.GetUser(ctx, "alice")
|
||||
require.NoError(t, err)
|
||||
require.Len(t, ident.Credentials, 1)
|
||||
assert.Equal(t, *out.CreateAccessKeyResult.AccessKey.AccessKeyId, ident.Credentials[0].AccessKey)
|
||||
}
|
||||
|
||||
// CreateAccessKey for a user the store does not know still needs the full save:
|
||||
// the handler creates the identity in the config, so there is no existing
|
||||
// record to write a key into.
|
||||
func TestIamApiCreateAccessKeyUnknownUserFallsBack(t *testing.T) {
|
||||
srv, cm, counter, _ := targetedIamApiServer()
|
||||
ctx := context.Background()
|
||||
|
||||
req, _ := iam.New(session.New()).CreateAccessKeyRequest(&iam.CreateAccessKeyInput{UserName: aws.String("ghost")})
|
||||
_ = req.Build()
|
||||
out := CreateAccessKeyResponse{}
|
||||
resp, err := executeRequestWith(srv, req.HTTPRequest, &out)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, resp.Code)
|
||||
|
||||
assert.Equal(t, 1, counter.putCalled)
|
||||
ident, err := cm.GetUser(ctx, "ghost")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, ident.Credentials, 1)
|
||||
}
|
||||
|
||||
func TestIamApiDeleteAccessKeyTargeted(t *testing.T) {
|
||||
srv, cm, counter, rec := targetedIamApiServer()
|
||||
ctx := context.Background()
|
||||
require.NoError(t, cm.CreateUser(ctx, &iam_pb.Identity{
|
||||
Name: "alice",
|
||||
Credentials: []*iam_pb.Credential{{AccessKey: "AKIATESTFAKEKEY00001", SecretKey: "testsecretfake"}},
|
||||
}))
|
||||
|
||||
req, _ := iam.New(session.New()).DeleteAccessKeyRequest(&iam.DeleteAccessKeyInput{
|
||||
UserName: aws.String("alice"),
|
||||
AccessKeyId: aws.String("AKIATESTFAKEKEY00001"),
|
||||
})
|
||||
_ = req.Build()
|
||||
out := DeleteAccessKeyResponse{}
|
||||
resp, err := executeRequestWith(srv, req.HTTPRequest, &out)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, resp.Code)
|
||||
|
||||
assert.Equal(t, 0, counter.putCalled, "DeleteAccessKey must not PutS3ApiConfiguration")
|
||||
assert.Equal(t, 1, rec.deleteAccessKeys)
|
||||
|
||||
ident, err := cm.GetUser(ctx, "alice")
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, ident.Credentials)
|
||||
|
||||
// Deleting a key that does not exist still answers 200 and stays targeted.
|
||||
req, _ = iam.New(session.New()).DeleteAccessKeyRequest(&iam.DeleteAccessKeyInput{
|
||||
UserName: aws.String("alice"),
|
||||
AccessKeyId: aws.String("AKIATESTFAKEKEY99999"),
|
||||
})
|
||||
_ = req.Build()
|
||||
resp, err = executeRequestWith(srv, req.HTTPRequest, &out)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, resp.Code)
|
||||
assert.Equal(t, 0, counter.putCalled)
|
||||
}
|
||||
|
||||
func TestIamApiUpdateAccessKeyTargeted(t *testing.T) {
|
||||
srv, cm, counter, rec := targetedIamApiServer()
|
||||
ctx := context.Background()
|
||||
require.NoError(t, cm.CreateUser(ctx, &iam_pb.Identity{
|
||||
Name: "alice",
|
||||
Credentials: []*iam_pb.Credential{{AccessKey: "AKIATESTFAKEKEY00001", SecretKey: "testsecretfake", Status: "Active"}},
|
||||
}))
|
||||
|
||||
req, _ := iam.New(session.New()).UpdateAccessKeyRequest(&iam.UpdateAccessKeyInput{
|
||||
UserName: aws.String("alice"),
|
||||
AccessKeyId: aws.String("AKIATESTFAKEKEY00001"),
|
||||
Status: aws.String(iam.StatusTypeInactive),
|
||||
})
|
||||
_ = req.Build()
|
||||
out := UpdateAccessKeyResponse{}
|
||||
resp, err := executeRequestWith(srv, req.HTTPRequest, &out)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, resp.Code)
|
||||
|
||||
assert.Equal(t, 0, counter.putCalled, "UpdateAccessKey must not PutS3ApiConfiguration")
|
||||
assert.Equal(t, 1, rec.updateUsers)
|
||||
|
||||
ident, err := cm.GetUser(ctx, "alice")
|
||||
require.NoError(t, err)
|
||||
require.Len(t, ident.Credentials, 1)
|
||||
assert.Equal(t, iam.StatusTypeInactive, ident.Credentials[0].Status)
|
||||
}
|
||||
|
||||
func TestIamApiPutUserPolicyTargeted(t *testing.T) {
|
||||
srv, cm, counter, rec := targetedIamApiServer()
|
||||
ctx := context.Background()
|
||||
require.NoError(t, cm.CreateUser(ctx, &iam_pb.Identity{Name: "alice"}))
|
||||
require.NoError(t, cm.CreateUser(ctx, &iam_pb.Identity{Name: "bob"}))
|
||||
|
||||
req, _ := iam.New(session.New()).PutUserPolicyRequest(&iam.PutUserPolicyInput{
|
||||
UserName: aws.String("alice"),
|
||||
PolicyName: aws.String("read-bench"),
|
||||
PolicyDocument: aws.String(targetedTestPolicy),
|
||||
})
|
||||
_ = req.Build()
|
||||
out := PutUserPolicyResponse{}
|
||||
resp, err := executeRequestWith(srv, req.HTTPRequest, &out)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, resp.Code)
|
||||
|
||||
assert.Equal(t, 0, counter.putCalled, "PutUserPolicy must not PutS3ApiConfiguration")
|
||||
assert.Equal(t, 0, rec.saves)
|
||||
assert.Equal(t, 1, rec.updateUsers)
|
||||
|
||||
ident, err := cm.GetUser(ctx, "alice")
|
||||
require.NoError(t, err)
|
||||
assert.NotEmpty(t, ident.Actions, "recomputed Actions must be persisted for the one identity")
|
||||
}
|
||||
|
||||
func TestIamApiDeleteUserPolicyTargeted(t *testing.T) {
|
||||
srv, cm, counter, rec := targetedIamApiServer()
|
||||
ctx := context.Background()
|
||||
require.NoError(t, cm.CreateUser(ctx, &iam_pb.Identity{Name: "alice"}))
|
||||
|
||||
putReq, _ := iam.New(session.New()).PutUserPolicyRequest(&iam.PutUserPolicyInput{
|
||||
UserName: aws.String("alice"),
|
||||
PolicyName: aws.String("read-bench"),
|
||||
PolicyDocument: aws.String(targetedTestPolicy),
|
||||
})
|
||||
_ = putReq.Build()
|
||||
putOut := PutUserPolicyResponse{}
|
||||
resp, err := executeRequestWith(srv, putReq.HTTPRequest, &putOut)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, resp.Code)
|
||||
|
||||
delReq, _ := iam.New(session.New()).DeleteUserPolicyRequest(&iam.DeleteUserPolicyInput{
|
||||
UserName: aws.String("alice"),
|
||||
PolicyName: aws.String("read-bench"),
|
||||
})
|
||||
_ = delReq.Build()
|
||||
delOut := DeleteUserPolicyResponse{}
|
||||
resp, err = executeRequestWith(srv, delReq.HTTPRequest, &delOut)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, resp.Code)
|
||||
|
||||
assert.Equal(t, 0, counter.putCalled, "DeleteUserPolicy must not PutS3ApiConfiguration")
|
||||
assert.Equal(t, 2, rec.updateUsers)
|
||||
|
||||
policies := Policies{}
|
||||
require.NoError(t, srv.s3ApiConfig.GetPolicies(&policies))
|
||||
assert.Empty(t, policies.InlinePolicies["alice"])
|
||||
}
|
||||
|
||||
func TestIamApiUpdateUserRenameTargeted(t *testing.T) {
|
||||
srv, cm, counter, _ := targetedIamApiServer()
|
||||
ctx := context.Background()
|
||||
require.NoError(t, cm.CreateUser(ctx, &iam_pb.Identity{Name: "alice"}))
|
||||
require.NoError(t, cm.CreateUser(ctx, &iam_pb.Identity{Name: "bob"}))
|
||||
require.NoError(t, cm.CreateGroup(ctx, &iam_pb.Group{Name: "devs", Members: []string{"alice", "bob"}}))
|
||||
|
||||
req, _ := iam.New(session.New()).UpdateUserRequest(&iam.UpdateUserInput{
|
||||
UserName: aws.String("alice"),
|
||||
NewUserName: aws.String("alice-renamed"),
|
||||
})
|
||||
_ = req.Build()
|
||||
out := UpdateUserResponse{}
|
||||
resp, err := executeRequestWith(srv, req.HTTPRequest, &out)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, resp.Code)
|
||||
|
||||
assert.Equal(t, 0, counter.putCalled, "UpdateUser rename must not PutS3ApiConfiguration")
|
||||
|
||||
_, err = cm.GetUser(ctx, "alice")
|
||||
require.ErrorIs(t, err, credential.ErrUserNotFound)
|
||||
_, err = cm.GetUser(ctx, "alice-renamed")
|
||||
require.NoError(t, err)
|
||||
|
||||
g, err := cm.GetGroup(ctx, "devs")
|
||||
require.NoError(t, err)
|
||||
assert.ElementsMatch(t, []string{"alice-renamed", "bob"}, g.Members)
|
||||
}
|
||||
|
||||
func TestIamApiDeleteUserTargeted(t *testing.T) {
|
||||
srv, cm, counter, rec := targetedIamApiServer()
|
||||
ctx := context.Background()
|
||||
require.NoError(t, cm.CreateUser(ctx, &iam_pb.Identity{Name: "alice"}))
|
||||
require.NoError(t, cm.CreateUser(ctx, &iam_pb.Identity{Name: "bob"}))
|
||||
require.NoError(t, cm.CreateGroup(ctx, &iam_pb.Group{Name: "devs", Members: []string{"alice", "bob"}}))
|
||||
|
||||
req, _ := iam.New(session.New()).DeleteUserRequest(&iam.DeleteUserInput{UserName: aws.String("alice")})
|
||||
_ = req.Build()
|
||||
out := DeleteUserResponse{}
|
||||
resp, err := executeRequestWith(srv, req.HTTPRequest, &out)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, resp.Code)
|
||||
|
||||
assert.Equal(t, 0, counter.putCalled, "DeleteUser must not PutS3ApiConfiguration")
|
||||
assert.Equal(t, 1, rec.deleteUsers)
|
||||
assert.Equal(t, 1, rec.updateGroups)
|
||||
|
||||
_, err = cm.GetUser(ctx, "alice")
|
||||
require.ErrorIs(t, err, credential.ErrUserNotFound)
|
||||
g, err := cm.GetGroup(ctx, "devs")
|
||||
require.NoError(t, err)
|
||||
assert.ElementsMatch(t, []string{"bob"}, g.Members)
|
||||
_, err = cm.GetUser(ctx, "bob")
|
||||
require.NoError(t, err, "other identities must be untouched")
|
||||
}
|
||||
+23
-32
@@ -17,9 +17,11 @@ import (
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/seaweedfs/seaweedfs/weed/credential"
|
||||
"github.com/seaweedfs/seaweedfs/weed/credential/memory"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api"
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/policy_engine"
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util/request_id"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -523,32 +525,13 @@ func executeRequest(req *http.Request, v interface{}) (*httptest.ResponseRecorde
|
||||
}
|
||||
|
||||
func TestHandleImplicitUsername(t *testing.T) {
|
||||
// Create a mock IamApiServer with credential store
|
||||
// The handleImplicitUsername function now looks up the username from the
|
||||
// credential store based on AccessKeyId, not from the region field in the auth header.
|
||||
// Note: Using obviously fake access keys to avoid CI secret scanner false positives
|
||||
// The implicit name is the authenticated identity recorded in the request
|
||||
// context by the auth middleware; request headers are never re-parsed.
|
||||
iama := &IamApiServer{}
|
||||
|
||||
// Create IAM directly as struct literal (same pattern as other tests)
|
||||
iam := &s3api.IdentityAccessManagement{}
|
||||
|
||||
// Load test credentials - map access key to identity name
|
||||
testConfig := &iam_pb.S3ApiConfiguration{
|
||||
Identities: []*iam_pb.Identity{
|
||||
{
|
||||
Name: "testuser1",
|
||||
Credentials: []*iam_pb.Credential{
|
||||
{AccessKey: "AKIATESTFAKEKEY000001", SecretKey: "testsecretfake"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
err := iam.LoadS3ApiConfigurationFromBytes(mustMarshalJSON(t, testConfig))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load test config: %v", err)
|
||||
}
|
||||
|
||||
iama := &IamApiServer{
|
||||
iam: iam,
|
||||
withIdentity := func(name string) *http.Request {
|
||||
r := &http.Request{Header: http.Header{}}
|
||||
return r.WithContext(s3_constants.SetIdentityNameInContext(r.Context(), name))
|
||||
}
|
||||
|
||||
var tests = []struct {
|
||||
@@ -556,14 +539,14 @@ func TestHandleImplicitUsername(t *testing.T) {
|
||||
values url.Values
|
||||
userName string
|
||||
}{
|
||||
// No authorization header - should not set username
|
||||
// No authenticated identity - should not set username
|
||||
{&http.Request{}, url.Values{}, ""},
|
||||
// Valid auth header with known access key - should look up and find "testuser1"
|
||||
{&http.Request{Header: http.Header{"Authorization": []string{"AWS4-HMAC-SHA256 Credential=AKIATESTFAKEKEY000001/20220420/us-east-1/iam/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=fakesignature0123456789abcdef"}}}, url.Values{}, "testuser1"},
|
||||
// Malformed auth header (no Credential=) - should not set username
|
||||
{&http.Request{Header: http.Header{"Authorization": []string{"AWS4-HMAC-SHA256 =AKIATESTFAKEKEY000001/20220420/test1/iam/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=fakesignature0123456789abcdef"}}}, url.Values{}, ""},
|
||||
// Unknown access key - should not set username
|
||||
{&http.Request{Header: http.Header{"Authorization": []string{"AWS4-HMAC-SHA256 Credential=AKIATESTUNKNOWN000000/20220420/us-east-1/iam/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=fakesignature0123456789abcdef"}}}, url.Values{}, ""},
|
||||
// Authenticated identity in context - sets its name
|
||||
{withIdentity("testuser1"), url.Values{}, "testuser1"},
|
||||
// A client-controlled Authorization header naming another user is ignored
|
||||
{&http.Request{Header: http.Header{"Authorization": []string{"AWS4-HMAC-SHA256 Credential=AKIATESTFAKEKEY000001/20220420/us-east-1/iam/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=fakesignature0123456789abcdef"}}}, url.Values{}, ""},
|
||||
// An explicit UserName is left alone
|
||||
{withIdentity("testuser1"), url.Values{"UserName": {"other"}}, "other"},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
@@ -607,6 +590,7 @@ func iamApiServerWithCredentialManager() (*IamApiServer, *credential.CredentialM
|
||||
type countingConfigSaver struct {
|
||||
cm *credential.CredentialManager
|
||||
putCalled int
|
||||
policies Policies
|
||||
}
|
||||
|
||||
func (c *countingConfigSaver) GetS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) error {
|
||||
@@ -616,6 +600,8 @@ func (c *countingConfigSaver) GetS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfigura
|
||||
}
|
||||
s3cfg.Identities = config.Identities
|
||||
s3cfg.Policies = config.Policies
|
||||
s3cfg.Groups = config.Groups
|
||||
s3cfg.ServiceAccounts = config.ServiceAccounts
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -625,10 +611,15 @@ func (c *countingConfigSaver) PutS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfigura
|
||||
}
|
||||
|
||||
func (c *countingConfigSaver) GetPolicies(policies *Policies) error {
|
||||
*policies = c.policies
|
||||
if c.policies.Policies == nil && c.policies.InlinePolicies == nil {
|
||||
return filer_pb.ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *countingConfigSaver) PutPolicies(policies *Policies) error {
|
||||
c.policies = *policies
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/glog"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/security"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
|
||||
)
|
||||
@@ -101,6 +102,9 @@ func readChunkNeedle(fileUrl string, w io.Writer, offset int64, jwt string) (wri
|
||||
if offset > 0 {
|
||||
req.Header.Set("Range", fmt.Sprintf("bytes=%d-", offset))
|
||||
}
|
||||
if jwt != "" {
|
||||
req.Header.Set("Authorization", security.BearerPrefix+jwt)
|
||||
}
|
||||
|
||||
resp, err := util_http.Do(req)
|
||||
if err != nil {
|
||||
@@ -118,7 +122,7 @@ func readChunkNeedle(fileUrl string, w io.Writer, offset int64, jwt string) (wri
|
||||
case http.StatusPartialContent:
|
||||
break
|
||||
default:
|
||||
return written, fmt.Errorf("Read chunk needle error: [%d] %s", resp.StatusCode, fileUrl)
|
||||
return written, util_http.StatusError(fileUrl, resp)
|
||||
|
||||
}
|
||||
return io.Copy(w, resp.Body)
|
||||
|
||||
Loaded 100 of 283 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user