-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
69 lines (51 loc) · 2.05 KB
/
main.go
File metadata and controls
69 lines (51 loc) · 2.05 KB
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
package main
import (
"context"
"distributedKeyValue/persistence"
"distributedKeyValue/server"
servicediscovery "distributedKeyValue/service_discovery"
twophasecommitcoordinator "distributedKeyValue/two_phase_commit_coordinator"
twophasecommitparticipant "distributedKeyValue/two_phase_commit_participant"
"log/slog"
"os"
"time"
"github.com/charmbracelet/log"
"github.com/joho/godotenv"
)
const transactionStatesPath = "transactionsStates.gob"
func main() {
err := godotenv.Load()
if err != nil {
log.Warn("No .env file present (fine for compse setup)")
}
ownName := os.Getenv("NODE_NAME")
clog := log.NewWithOptions(os.Stderr, log.Options{
ReportTimestamp: true, // Ensure this is true
TimeFormat: time.Kitchen, // "3:04PM" or use time.RFC3339
Level: log.DebugLevel,
})
// 2. Wrap it in slog
logger := slog.New(clog)
slog.SetDefault(logger)
sqliteModule := persistence.MustNewSqliteTransactionManagerPersistence(ownName + "data.sqlite")
servicediscoveryModule := servicediscovery.NewEnvServiceDiscovery()
twoPhaseCommitCoordinator := &twophasecommitcoordinator.TwoPhaseCommit{
PersistenceManager: sqliteModule,
SDiscovery: servicediscoveryModule, // we will set this later after we initialize the service discovery module
}
twoPhaseCommitParticipant := &twophasecommitparticipant.TwoPhaseCommitParticipant{
PersistenceManager: sqliteModule,
}
// Setup channel manager
channelManager := twophasecommitcoordinator.NewChannelManager()
// Setup background runners
ctxPhase1, cancelPhase1 := context.WithCancel(context.Background())
phase1Runner := twophasecommitcoordinator.GetNewPhase1Runner(sqliteModule, servicediscoveryModule, channelManager)
go phase1Runner(ctxPhase1)
defer cancelPhase1()
ctxPhase2, cancelPhase2 := context.WithCancel(context.Background())
phase2Runner := twophasecommitcoordinator.GetNewPhase2Runner(sqliteModule, servicediscoveryModule)
go phase2Runner(ctxPhase2)
defer cancelPhase2()
server.StartServer(twoPhaseCommitCoordinator, twoPhaseCommitParticipant, channelManager)
}