Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State Route #543

Merged
merged 13 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .github/workflows/publish - zen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ jobs:
- uses: actions/setup-go@v3
with:
go-version: 'stable'
- name: Install dependencies
- name: Setup
run: |
sudo apt update && sudo apt install -y gcc-aarch64-linux-gnu
sudo apt update
sudo apt install -y gcc-aarch64-linux-gnu
go generate ./...
- name: Build amd64
env:
CGO_ENABLED: 1
Expand Down Expand Up @@ -125,6 +127,9 @@ jobs:
security import $APPLE_CERT_PATH -P $APPLE_CERT_PASSWORD -A -t cert -f pkcs12 -k $KEYCHAIN_PATH
security find-identity -v $KEYCHAIN_PATH -p codesigning
security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k $APPLE_KEYCHAIN_PASSWORD $KEYCHAIN_PATH

# generate
go generate ./...
- name: Build amd64
env:
APPLE_CERT_ID: ${{ secrets.APPLE_CERT_ID }}
Expand Down Expand Up @@ -176,6 +181,9 @@ jobs:
- uses: actions/setup-go@v3
with:
go-version: 'stable'
- name: Setup
shell: bash
run: go generate ./...
- name: Build amd64
env:
CGO_ENABLED: 1
Expand Down
13 changes: 11 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ jobs:
- uses: actions/setup-go@v3
with:
go-version: 'stable'
- name: Install dependencies
- name: Setup
run: |
sudo apt update && sudo apt install -y gcc-aarch64-linux-gnu
sudo apt update
sudo apt install -y gcc-aarch64-linux-gnu
go generate ./...
- name: Build amd64
env:
CGO_ENABLED: 1
Expand Down Expand Up @@ -123,6 +125,9 @@ jobs:
security import $APPLE_CERT_PATH -P $APPLE_CERT_PASSWORD -A -t cert -f pkcs12 -k $KEYCHAIN_PATH
security find-identity -v $KEYCHAIN_PATH -p codesigning
security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k $APPLE_KEYCHAIN_PASSWORD $KEYCHAIN_PATH

# generate
go generate ./...
- name: Build amd64
env:
APPLE_CERT_ID: ${{ secrets.APPLE_CERT_ID }}
Expand Down Expand Up @@ -174,6 +179,10 @@ jobs:
- uses: actions/setup-go@v3
with:
go-version: 'stable'
- name: Setup
shell: bash
run: |
go generate ./...
- name: Build amd64
env:
CGO_ENABLED: 1
Expand Down
9 changes: 9 additions & 0 deletions api/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ type (
UptimeMS ParamDuration `json:"uptimeMS"`
}

// AutopilotStateResponse is the response type for the /autopilot/state
// endpoint.
AutopilotStateResponse struct {
AutopilotStatusResponse // TODO: deprecate /autopilot/status

StartTime time.Time `json:"startTime"`
BuildState
}

HostHandlerResponseChecks struct {
Gouging bool `json:"gouging"`
GougingBreakdown HostGougingBreakdown `json:"gougingBreakdown"`
Expand Down
6 changes: 6 additions & 0 deletions api/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ type AccountHandlerPOST struct {
HostKey types.PublicKey `json:"hostKey"`
}

// BusStateResponse is the response type for the /bus/state endpoint.
type BusStateResponse struct {
StartTime time.Time `json:"startTime"`
BuildState
}

// ConsensusState holds the current blockheight and whether we are synced or not.
type ConsensusState struct {
BlockHeight uint64 `json:"blockHeight"`
Expand Down
14 changes: 14 additions & 0 deletions api/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package api

import "time"

type (
// BuildState contains static information about the build.
BuildState struct {
Network string `json:"network"`
Version string `json:"version"`
Commit string `json:"commit"`
OS string `json:"OS"`
BuildTime time.Time `json:"buildTime"`
}
)
7 changes: 7 additions & 0 deletions api/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ type UploaderStats struct {
AvgSectorUploadSpeedMBPS float64 `json:"avgSectorUploadSpeedMBPS"`
}

// WorkerStateResponse is the response type for the /worker/state endpoint.
type WorkerStateResponse struct {
ID string `json:"id"`
StartTime time.Time `json:"startTime"`
BuildState
}

// An UploadOption overrides an option on the upload and migrate endpoints in
// the worker.
type UploadOption func(url.Values)
Expand Down
56 changes: 47 additions & 9 deletions autopilot/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"runtime"
"strings"
"sync"
"time"
Expand All @@ -16,6 +17,7 @@ import (
"go.sia.tech/jape"
"go.sia.tech/renterd/alerts"
"go.sia.tech/renterd/api"
"go.sia.tech/renterd/build"
"go.sia.tech/renterd/hostdb"
"go.sia.tech/renterd/object"
"go.sia.tech/renterd/tracing"
Expand Down Expand Up @@ -110,11 +112,11 @@ type Autopilot struct {
tickerDuration time.Duration
wg sync.WaitGroup

startStopMu sync.Mutex
runningSince time.Time
stopChan chan struct{}
ticker *time.Ticker
triggerChan chan bool
startStopMu sync.Mutex
startTime time.Time
stopChan chan struct{}
ticker *time.Ticker
triggerChan chan bool
}

// state holds a bunch of variables that are used by the autopilot and updated
Expand Down Expand Up @@ -171,6 +173,7 @@ func (ap *Autopilot) Handler() http.Handler {
"POST /hosts": ap.hostsHandlerPOST,
"GET /host/:hostKey": ap.hostHandlerGET,
"GET /status": ap.statusHandlerGET,
"GET /state": ap.stateHandlerGET,
}))
}

Expand All @@ -180,7 +183,7 @@ func (ap *Autopilot) Run() error {
ap.startStopMu.Unlock()
return errors.New("already running")
}
ap.runningSince = time.Now()
ap.startTime = time.Now()
ap.stopChan = make(chan struct{})
ap.triggerChan = make(chan bool)
ap.ticker = time.NewTicker(ap.tickerDuration)
Expand Down Expand Up @@ -296,7 +299,7 @@ func (ap *Autopilot) Shutdown(_ context.Context) error {
close(ap.stopChan)
close(ap.triggerChan)
ap.wg.Wait()
ap.runningSince = time.Time{}
ap.startTime = time.Time{}
}
return nil
}
Expand All @@ -319,11 +322,17 @@ func (ap *Autopilot) Trigger(forceScan bool) bool {
}
}

func (ap *Autopilot) StartTime() time.Time {
ap.startStopMu.Lock()
defer ap.startStopMu.Unlock()
return ap.startTime
}

func (ap *Autopilot) Uptime() (dur time.Duration) {
ap.startStopMu.Lock()
defer ap.startStopMu.Unlock()
if ap.isRunning() {
dur = time.Since(ap.runningSince)
dur = time.Since(ap.startTime)
}
return
}
Expand Down Expand Up @@ -389,7 +398,7 @@ func (ap *Autopilot) blockUntilSynced(interrupt <-chan time.Time) bool {
}

func (ap *Autopilot) isRunning() bool {
return !ap.runningSince.IsZero()
return !ap.startTime.IsZero()
}

func (ap *Autopilot) updateState(ctx context.Context) error {
Expand Down Expand Up @@ -601,6 +610,35 @@ func (ap *Autopilot) statusHandlerGET(jc jape.Context) {
})
}

func (ap *Autopilot) stateHandlerGET(jc jape.Context) {
migrating, mLastStart := ap.m.Status()
scanning, sLastStart := ap.s.Status()
_, err := ap.bus.Autopilot(jc.Request.Context(), ap.id)
if err != nil && !strings.Contains(err.Error(), api.ErrAutopilotNotFound.Error()) {
jc.Error(err, http.StatusInternalServerError)
return
}

jc.Encode(api.AutopilotStateResponse{
AutopilotStatusResponse: api.AutopilotStatusResponse{
Configured: err == nil,
Migrating: migrating,
MigratingLastStart: api.ParamTime(mLastStart),
Scanning: scanning,
ScanningLastStart: api.ParamTime(sLastStart),
UptimeMS: api.ParamDuration(ap.Uptime()),
},
StartTime: ap.StartTime(),
BuildState: api.BuildState{
Network: build.NetworkName(),
Version: build.Version(),
Commit: build.Commit(),
OS: runtime.GOOS,
BuildTime: build.BuildTime(),
},
})
}

func (ap *Autopilot) hostsHandlerPOST(jc jape.Context) {
var req api.SearchHostsRequest
if jc.Decode(&req) != nil {
Expand Down
6 changes: 6 additions & 0 deletions autopilot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ func (c *Client) HostInfos(ctx context.Context, filterMode, usabilityMode string
return
}

// State returns the current state of the autopilot.
func (c *Client) State() (state api.AutopilotStateResponse, err error) {
err = c.c.GET("/state", &state)
return
}

func (c *Client) Status() (resp api.AutopilotStatusResponse, err error) {
err = c.c.GET("/status", &resp)
return
Expand Down
4 changes: 0 additions & 4 deletions build/env.go → build/env_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@ package build
import (
"time"

"go.sia.tech/core/chain"
"go.sia.tech/core/types"
"go.sia.tech/renterd/api"
)

const (
ConsensusNetworkName = "Mainnet"
DefaultAPIAddress = "localhost:9980"
DefaultGatewayAddress = ":9981"
)

var (
ConsensusNetwork, _ = chain.Mainnet()

// DefaultGougingSettings define the default gouging settings the bus is
// configured with on startup. These values can be adjusted using the
// settings API.
Expand Down
4 changes: 0 additions & 4 deletions build/env_testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@ package build
import (
"time"

"go.sia.tech/core/chain"
"go.sia.tech/core/types"
"go.sia.tech/renterd/api"
)

const (
ConsensusNetworkName = "Testnet-Zen"
DefaultAPIAddress = "localhost:9880"
DefaultGatewayAddress = ":9881"
)

var (
ConsensusNetwork, _ = chain.TestnetZen()

// DefaultGougingSettings define the default gouging settings the bus is
// configured with on startup. These values can be adjusted using the
// settings API.
Expand Down
Loading