-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_scheduler.go
103 lines (91 loc) · 2.78 KB
/
disk_scheduler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//===----------------------------------------------------------------------===//
//
// 🚄 ElenaDB ®
//
// disk_scheduler.go
//
// Identification: pkg/storage/disk/disk_scheduler.go
//
// Copyright (c) 2024
//
//===----------------------------------------------------------------------===//
package storage_disk
import (
"fisi/elenadb/pkg/catalog"
"fisi/elenadb/pkg/common"
"fmt"
"sync"
)
// Represents a Write or Read request for the DiskManager to execute.
type DiskRequest struct {
// Flag indicating whether the request is a write or a read.
IsWrite bool
/*
* Pointer to the start of the memory location where a page is either:
* 1. being read into from disk (on a read).
* 2. being written out to disk (on a write).
*/
Data []byte
// ID of the page being read from / written to disk.
PageID common.PageID_t
// Channel used to signal to the request issuer when the request has been completed.
Callback chan bool
filename string
}
type DiskScheduler struct {
// Pointer to the disk manager.
diskManager *DiskManager
// A shared queue to concurrently schedule and process requests. When the DiskScheduler's destructor is called, `nil` is put into the queue to signal to the background thread to stop execution. */
RequestQueue common.Channel[*DiskRequest] // esto debe testearse fijo
// Mutex to synchronize access to shared resources. */
Mutex sync.Mutex
// Wait group for tracking the worker thread. */
WaitGroup sync.WaitGroup
Catalog *catalog.Catalog
}
// Don't forget to start worker threads.
func NewScheduler(dm *DiskManager, catalog *catalog.Catalog) *DiskScheduler {
return &DiskScheduler{
diskManager: dm,
// FLAG_ESTRUCTURA: queue
RequestQueue: *common.NewChannel[*DiskRequest](),
Catalog: catalog,
}
}
// FLAG_ALGORITMO: algoritmo FCFS (First-Come, First-Served) de planificación de disco.
func (ds *DiskScheduler) Schedule(request *DiskRequest) {
filename := ds.Catalog.FilenameFromFileId(request.PageID.GetFileId())
request.filename = *filename
ds.RequestQueue.Put(request)
}
func (ds *DiskScheduler) StartWorkerThread() {
go func() {
for {
request := ds.RequestQueue.Get()
if request == nil {
return
}
if request.IsWrite {
err := ds.diskManager.WritePage(request.PageID, request.Data, request.filename)
if err != nil {
fmt.Println("unexpedted I/O error:", err.Error())
request.Callback <- false
} else {
request.Callback <- true
}
} else {
data, err := ds.diskManager.ReadPage(request.PageID, request.filename)
if err != nil {
if err.Error() == "EOF" {
request.Callback <- false
} else {
panic(fmt.Sprintf("unexpected I/O error: %s", err.Error()))
}
} else {
copy(request.Data, data)
request.Callback <- true
}
}
}
}()
}