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

feat: add fsm for persistent cache peer #3563

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .github/workflows/compatibility-e2e-v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ jobs:
include:
- module: manager
image: manager
image-tag: v2.1.58
image-tag: v2.1.60
chart-name: manager
- module: scheduler
image: scheduler
image-tag: v2.1.58
image-tag: v2.1.60
chart-name: scheduler
- module: client
image: client
Expand Down
134 changes: 134 additions & 0 deletions scheduler/resource/persistentcache/peer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright 2024 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package persistentcache

import (
"context"
"time"

"github.com/bits-and-blooms/bitset"
"github.com/looplab/fsm"

logger "d7y.io/dragonfly/v2/internal/dflog"
)

const (
// Peer has been created but did not start running.
PeerStatePending = "Pending"

// Peer successfully registered and perpared to download.
PeerStateReceived = "Received"

// Peer is downloading resources from peer.
PeerStateRunning = "Running"

// Peer has been downloaded successfully.
PeerStateSucceeded = "Succeeded"

// Peer has been downloaded failed.
PeerStateFailed = "Failed"
)

const (
// Peer is registered and perpared to download.
PeerEventRegister = "Register"

// Peer is downloading.
PeerEventDownload = "Download"

// Peer downloaded successfully.
PeerEventDownloadSucceeded = "DownloadSucceeded"

// Peer downloaded failed.
PeerEventDownloadFailed = "DownloadFailed"
)

// Peer contains content for persistent cache peer.
type Peer struct {
// ID is persistent cache peer id.
ID string

// Pieces is finished pieces bitset.
FinishedPieces *bitset.BitSet

// Persistent cache peer state machine.
FSM *fsm.FSM

// Task is persistent cache task.
Task *Task

// Host is the peer host.
Host *Host

// BlockParents is bad parents ids.
BlockParents []string

// Cost is the cost of downloading.
Cost time.Duration

// CreatedAt is persistent cache peer create time.
CreatedAt time.Time

// UpdatedAt is persistent cache peer update time.
UpdatedAt time.Time

// Persistent cache peer log.
Log *logger.SugaredLoggerOnWith
}

// New persistent cache peer instance.
func NewPeer(id, state string, finishedPieces *bitset.BitSet, blockParents []string, task *Task, host *Host,
cost time.Duration, createdAt, updatedAt time.Time, log *logger.SugaredLoggerOnWith) *Peer {
p := &Peer{
ID: id,
FinishedPieces: finishedPieces,
Task: task,
Host: host,
BlockParents: blockParents,
Cost: cost,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
Log: logger.WithPeer(host.ID, task.ID, id),
}

// Initialize state machine.
p.FSM = fsm.NewFSM(
PeerStatePending,
fsm.Events{
{Name: PeerEventRegister, Src: []string{PeerStatePending}, Dst: PeerStateReceived},
{Name: PeerEventDownload, Src: []string{PeerStateReceived}, Dst: PeerStateRunning},
{Name: PeerEventDownloadSucceeded, Src: []string{PeerStateRunning}, Dst: PeerStateSucceeded},
{Name: PeerEventDownloadFailed, Src: []string{PeerStateRunning}, Dst: PeerStateFailed},
},
fsm.Callbacks{
PeerEventRegister: func(ctx context.Context, e *fsm.Event) {
p.Log.Infof("peer state is %s", e.FSM.Current())
},
PeerEventDownload: func(ctx context.Context, e *fsm.Event) {
p.Log.Infof("peer state is %s", e.FSM.Current())
},
PeerEventDownloadSucceeded: func(ctx context.Context, e *fsm.Event) {
p.Log.Infof("peer state is %s", e.FSM.Current())
},
PeerEventDownloadFailed: func(ctx context.Context, e *fsm.Event) {
p.Log.Infof("peer state is %s", e.FSM.Current())
},
},
)

return p
}
2 changes: 1 addition & 1 deletion scheduler/resource/standard/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ type Peer struct {
// Used only in v2 version of the grpc.
AnnouncePeerStream *atomic.Value

// Task state machine.
// Peer state machine.
FSM *fsm.FSM

// Task is peer task.
Expand Down
Loading