Skip to content

Commit 6f8f0a4

Browse files
Sankara-Jeffersonparkan
authored andcommitted
Fix ci and benchmarks (#581)
This PR addresses several critical areas focusing on CI reliability, infrastructure improvements, and code quality enhancements. The changes fall into four main categories: **Original CI Failures:** - Tests were randomly failing due to port conflicts between concurrent database instances - Multiple test suites trying to use port 27017 for MongoDB simultaneously - MySQL and PostgreSQL tests colliding on their default ports - These conflicts caused connection timeouts and "address already in use" errors - The CI builds were failing due to a combination of static analysis errors and inconsistent code formatting, which also impacted overall code maintainability. - Previous: Manual documentation led to code/doc drift **Root Cause Analysis:** - IPDX workflows were starting database services without configurable ports - Our custom tests were also starting databases on default ports - When both ran together, or when parallel tests ran, they would conflict - No proper cleanup between test runs meant ports could stay occupied **Problem Impact:** - CI builds were becoming unreliable and flaky - PRs required multiple retries and still failed - Test failures weren't actual code issues but infrastructure problems **Implementation** **CI/Testing Infrastructure** - Improved CI workflow configuration with better caching strategies - Added MongoDB integration test support and documentation - Implemented cache cleanup workflow - Enhanced test stability and reliability **Code Quality & Architecture** - Refactored client code structure and API organization - Improved thread safety in analytics package - Enhanced error handling and type safety - Fixed duration handling in client configuration **Documentation** - Added integration test requirements documentation - Improved API documentation - Reorganized documentation generation - Restored original docgen.sh functionality and regenerated complete CLI documentation with proper environment variable handling **Dependencies** - Updated multiple dependencies to newer versions - Removed unused dependencies **Note**: I will address the risks and Mitigations in this [issue](#582) . Kindly add anything missing on the issue as a comment to the PR for reference. I will lift them to the issue when creating a new pr for them.
1 parent f70df65 commit 6f8f0a4

File tree

308 files changed

+583
-329
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

308 files changed

+583
-329
lines changed

.github/actions/go-check-setup/action.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ runs:
99
path: |
1010
~/.cache/go-build
1111
~/go/pkg/mod
12-
key: ${{ matrix.os }}-golang-${{ matrix.go }}-${{ hashFiles('**/go.sum') }}
12+
key: ${{ matrix.os }}-golang-${{ matrix.go }}-${{ hashFiles('**/go.sum') }}-${{ github.ref }}-${{ github.sha }}
1313
restore-keys: |
14+
${{ matrix.os }}-golang-${{ matrix.go }}-${{ hashFiles('**/go.sum') }}-${{ github.ref }}-
15+
${{ matrix.os }}-golang-${{ matrix.go }}-${{ hashFiles('**/go.sum') }}-
1416
${{ matrix.os }}-golang-${{ matrix.go }}-
1517
1618
- name: Setup Go

.github/actions/go-test-setup/action.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@ description: Setup Cache
33
runs:
44
using: "composite"
55
steps:
6+
- name: Clean Go cache directories
7+
shell: bash
8+
run: |
9+
rm -rf ~/.cache/go-build
10+
rm -rf ~/go/pkg/mod
611
- name: Setup Golang caches
712
uses: actions/cache@v4
813
with:
914
path: |
1015
~/.cache/go-build
1116
~/go/pkg/mod
12-
key: ${{ matrix.os }}-golang-${{ matrix.go }}-${{ hashFiles('**/go.sum') }}
17+
key: ${{ matrix.os }}-golang-${{ matrix.go }}-${{ hashFiles('**/go.sum') }}-${{ github.ref }}-${{ github.sha }}
1318
restore-keys: |
19+
${{ matrix.os }}-golang-${{ matrix.go }}-${{ hashFiles('**/go.sum') }}-${{ github.ref }}-
20+
${{ matrix.os }}-golang-${{ matrix.go }}-${{ hashFiles('**/go.sum') }}-
1421
${{ matrix.os }}-golang-${{ matrix.go }}-
1522
- name: Setup PostgreSQL database
1623
uses: ikalnytskyi/action-setup-postgres@v6
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Cache Cleanup
2+
on:
3+
workflow_dispatch: # Manual trigger
4+
schedule:
5+
- cron: '0 0 * * 0' # Run weekly on Sunday at midnight
6+
7+
jobs:
8+
cleanup:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Cleanup
12+
run: |
13+
# Get all cache keys
14+
CACHE_KEYS=$(gh cache list -L 1000 | awk '{print $1}')
15+
16+
# Calculate total size
17+
TOTAL_SIZE=$(gh cache list -L 1000 | awk '{sum += $2} END {print sum}')
18+
19+
# If total size > 8GB (keeping buffer from 10GB limit)
20+
if [ "$TOTAL_SIZE" -gt 8000000000 ]; then
21+
# Delete older caches until we're under 8GB
22+
echo "$CACHE_KEYS" | while read key; do
23+
gh cache delete "$key" -f
24+
TOTAL_SIZE=$(gh cache list -L 1000 | awk '{sum += $2} END {print sum}')
25+
if [ "$TOTAL_SIZE" -lt 8000000000 ]; then
26+
break
27+
fi
28+
done
29+
fi
30+
env:
31+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/docgen.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Documentation Generation
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches: [main]
7+
pull_request:
8+
branches: [main]
9+
10+
jobs:
11+
docgen:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
- name: Setup Go
17+
uses: actions/setup-go@v5
18+
with:
19+
go-version-file: 'go.mod'
20+
- name: Initialize database for doc generation
21+
run: |
22+
cd singularity
23+
./singularity admin init
24+
- name: Generate documentation
25+
run: |
26+
cd singularity
27+
sh docgen.sh

.github/workflows/go-check.yml

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ name: Go Checks
22

33
on:
44
pull_request:
5+
branches: [main, develop]
56
push:
6-
branches: ["main"]
7+
branches: [main, develop]
78
workflow_dispatch:
89

910
permissions:
@@ -14,19 +15,88 @@ concurrency:
1415
cancel-in-progress: true
1516

1617
jobs:
17-
go-check:
18-
uses: ipdxco/unified-github-workflows/.github/workflows/[email protected]
18+
go-check-all:
19+
name: go-check / All
20+
runs-on: ubuntu-latest
21+
services:
22+
mysql:
23+
image: mysql:8.0
24+
env:
25+
MYSQL_ROOT_PASSWORD: root
26+
MYSQL_DATABASE: singularity
27+
MYSQL_USER: singularity
28+
MYSQL_PASSWORD: singularity
29+
ports:
30+
- 3306:3306
31+
options: >-
32+
--health-cmd="mysqladmin ping"
33+
--health-interval=10s
34+
--health-timeout=5s
35+
--health-retries=3
36+
37+
postgres:
38+
image: postgres:15
39+
env:
40+
POSTGRES_USER: singularity
41+
POSTGRES_PASSWORD: singularity
42+
POSTGRES_DB: singularity
43+
ports:
44+
- 5432:5432
45+
options: >-
46+
--health-cmd pg_isready
47+
--health-interval 10s
48+
--health-timeout 5s
49+
--health-retries 5
50+
51+
steps:
52+
- uses: actions/checkout@v3
53+
54+
- name: Set up Go
55+
uses: actions/setup-go@v4
56+
with:
57+
go-version: "1.21.x"
58+
59+
- name: Wait for PostgreSQL
60+
run: |
61+
echo "Waiting for PostgreSQL..."
62+
for i in {1..10}; do
63+
if PGPASSWORD=singularity psql -h localhost -U singularity -d singularity -c "SELECT 1" > /dev/null 2>&1; then
64+
echo "Postgres is ready!"
65+
break
66+
fi
67+
sleep 3
68+
done
69+
70+
- name: Verify MySQL connection
71+
run: mysql -h127.0.0.1 -P3306 -usingularity -psingularity -e "SELECT VERSION();"
72+
73+
- name: Verify PostgreSQL connection
74+
run: PGPASSWORD=singularity psql -h localhost -U singularity -d singularity -c "SELECT version();"
75+
76+
- name: Ensure swagger directories exist
77+
run: mkdir -p client/swagger/client
78+
79+
- name: Install swagger tools
80+
run: go install github.com/go-swagger/go-swagger/cmd/[email protected]
81+
82+
- name: Generate code
83+
run: go generate ./client/swagger/...
84+
85+
- name: Build
86+
run: go build ./...
87+
88+
- name: Run tests
89+
run: go test -v ./...
1990

2091
staticcheck:
2192
runs-on: ubuntu-latest
2293
steps:
23-
- name: Checkout code
24-
uses: actions/checkout@v3
94+
- uses: actions/checkout@v3
2595

2696
- name: Set up Go
2797
uses: actions/setup-go@v4
2898
with:
29-
go-version: "1.21"
99+
go-version: "1.21.x"
30100

31101
- name: Install staticcheck
32102
run: go install honnef.co/go/tools/cmd/staticcheck@latest

.github/workflows/go-test.yml

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ name: Go Test
22

33
on:
44
pull_request:
5+
branches: [main, develop]
56
push:
6-
branches: ["main"]
7+
branches: [main, develop]
78
workflow_dispatch:
89

910
permissions:
@@ -14,5 +15,50 @@ concurrency:
1415
cancel-in-progress: true
1516

1617
jobs:
17-
go-test:
18-
uses: ipdxco/unified-github-workflows/.github/workflows/[email protected]
18+
go-test-this:
19+
name: go-test / ${{ matrix.os }} (go this)
20+
runs-on: ${{ matrix.os }}
21+
strategy:
22+
matrix:
23+
os: [ubuntu-latest, macos-latest, windows-latest]
24+
steps:
25+
- uses: actions/checkout@v3
26+
27+
- name: Start MongoDB
28+
if: runner.os == 'Linux'
29+
uses: supercharge/[email protected]
30+
with:
31+
mongodb-version: '6.0'
32+
mongodb-port: 27018
33+
34+
- name: Set up Go
35+
uses: actions/setup-go@v4
36+
with:
37+
go-version: '1.21'
38+
39+
- name: Run Go Tests
40+
run: go test ./...
41+
42+
go-test-next:
43+
name: go-test / ${{ matrix.os }} (go next)
44+
runs-on: ${{ matrix.os }}
45+
strategy:
46+
matrix:
47+
os: [ubuntu-latest, macos-latest, windows-latest]
48+
steps:
49+
- uses: actions/checkout@v3
50+
51+
- name: Start MongoDB
52+
if: runner.os == 'Linux'
53+
uses: supercharge/[email protected]
54+
with:
55+
mongodb-version: '6.0'
56+
mongodb-port: 27018
57+
58+
- name: Set up Go
59+
uses: actions/setup-go@v4
60+
with:
61+
go-version: '1.21'
62+
63+
- name: Run Go Tests
64+
run: go test ./...

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,16 @@ The internal tool used by `js-singularity` to regenerate the CAR that captures t
427427

428428
## License
429429
Dual-licensed under [MIT](https://github.com/filecoin-project/lotus/blob/master/LICENSE-MIT) + [Apache 2.0](https://github.com/filecoin-project/lotus/blob/master/LICENSE-APACHE)
430+
431+
## Integration Tests & MongoDB
432+
433+
Some integration tests require a MongoDB instance running on `localhost:27018`.
434+
435+
- **CI:** MongoDB is automatically started on port 27018 in GitHub Actions workflows.
436+
- **Local Development:** You must start MongoDB locally on port 27018 before running tests:
437+
438+
```bash
439+
mongod --port 27018
440+
```
441+
442+
If MongoDB is not available, related tests will be skipped or fail with a connection error.

analytics/analytics.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import (
2121

2222
const flushInterval = time.Hour
2323

24-
var Enabled = true
24+
var (
25+
mu sync.RWMutex
26+
Enabled = true
27+
)
2528

2629
var logger = log.Logger("analytics")
2730

@@ -37,6 +40,8 @@ var logger = log.Logger("analytics")
3740
// Returns:
3841
// - An error if there are issues fetching the instance id from the database or if the database appears empty.
3942
func Init(ctx context.Context, db *gorm.DB) error {
43+
mu.Lock()
44+
defer mu.Unlock()
4045
if Instance != "" {
4146
return nil
4247
}
@@ -68,6 +73,27 @@ var (
6873
Identity string
6974
)
7075

76+
// GetInstance safely returns the Instance value
77+
func GetInstance() string {
78+
mu.RLock()
79+
defer mu.RUnlock()
80+
return Instance
81+
}
82+
83+
// GetIdentity safely returns the Identity value
84+
func GetIdentity() string {
85+
mu.RLock()
86+
defer mu.RUnlock()
87+
return Identity
88+
}
89+
90+
// IsEnabled safely returns the Enabled value
91+
func IsEnabled() bool {
92+
mu.RLock()
93+
defer mu.RUnlock()
94+
return Enabled
95+
}
96+
7197
type Collector struct {
7298
mu sync.Mutex
7399
packJobEvents []PackJobEvent

0 commit comments

Comments
 (0)