-
Notifications
You must be signed in to change notification settings - Fork 2
/
mustsync.go
151 lines (127 loc) · 4.21 KB
/
mustsync.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package sync
import (
"crypto/sha1"
"fmt"
"log"
"path/filepath"
"reflect"
"github.com/quic-s/quics-client/pkg/db/badger"
"github.com/quic-s/quics-client/pkg/net/qclient"
"github.com/quic-s/quics-client/pkg/types"
qp "github.com/quic-s/quics-protocol"
qstypes "github.com/quic-s/quics/pkg/types"
)
func NeedContentMain() {
err := QPClient.RecvTransactionHandleFunc(qstypes.NEEDCONTENT, func(conn *qp.Connection, stream *qp.Stream, transactionName string, transactionID []byte) error {
req, err := qclient.NeedContentRecvHandler(stream)
if err != nil {
return err
}
// get paths
afterPath := req.AfterPath
beforePath := badger.GetBeforePathWithAfterPath(afterPath)
path := filepath.Join(beforePath, afterPath)
syncMeta := badger.GetSyncMetadata(path)
if reflect.ValueOf(syncMeta).IsZero() {
return fmt.Errorf("cannot find sync metadata")
}
if syncMeta.LastUpdateTimestamp == req.LastUpdateTimestamp && syncMeta.LastUpdateHash == req.LastUpdateHash {
err := qclient.NeedContentHandler(stream, path, badger.GetUUID(), afterPath, syncMeta.LastUpdateTimestamp, syncMeta.LastUpdateHash)
if err != nil {
return err
}
}
return nil
})
if err != nil {
log.Println("[quics-client : NEEDCONTENT] ", err)
}
}
// MustSyncMain is a function to handle MustSync transaction
func MustSyncMain() {
err := QPClient.RecvTransactionHandleFunc(qstypes.MUSTSYNC, func(conn *qp.Connection, stream *qp.Stream, transactionName string, transactionID []byte) error {
UUID := badger.GetUUID()
mustSyncReq, err := qclient.MustSyncRecvHandler(stream)
if err != nil {
return err
}
// get paths
afterPath := mustSyncReq.AfterPath
beforePath := badger.GetBeforePathWithAfterPath(afterPath)
// lock mutex for each path
h := sha1.New()
h.Write([]byte(afterPath))
hash := h.Sum(nil)
PSMut[uint8(hash[0]%PSMutModNum)].Lock()
defer PSMut[uint8(hash[0]%PSMutModNum)].Unlock()
path := filepath.Join(beforePath, afterPath)
//If New File in coming, then make new sync meta in badger
// e.g. new file created by other clients
// if !badger.IsSyncMetadataExisted(path) {
// syncMetadata := types.SyncMetadata{
// BeforePath: beforePath,
// AfterPath: afterPath,
// LastUpdateTimestamp: 0,
// LastUpdateHash: "",
// LastSyncTimestamp: 0,
// LastSyncHash: "",
// }
// err := badger.Update(path, syncMetadata.Encode())
// if err != nil {
// return err
// }
// }
syncMetadata := badger.GetSyncMetadata(path)
if !CheckCanOverwrite(syncMetadata.LastSyncTimestamp, syncMetadata.LastUpdateTimestamp, mustSyncReq.LatestSyncTimestamp) {
err := qclient.MustSyncHandler(stream, UUID, "", 0, "")
if err != nil {
return err
}
return fmt.Errorf("transaction fail, cannot overwrite")
} else {
err = qclient.MustSyncHandler(stream, UUID, afterPath, mustSyncReq.LatestSyncTimestamp, mustSyncReq.LatestHash)
if err != nil {
return err
}
}
// Get File Contents
IsRemoved := false
// when "event remove" broadcasted, then do not save the file
if mustSyncReq.LatestHash == "" {
IsRemoved = true
}
_, err = qclient.GiveYouRecvHandler(stream, path, afterPath, mustSyncReq.LatestHash, IsRemoved)
if err != nil {
return err
}
if IsRemoved {
badger.Delete(path)
} else {
//update syncmeta
updatedSyncMeta := types.SyncMetadata{
BeforePath: beforePath,
AfterPath: afterPath,
LastUpdateTimestamp: mustSyncReq.LatestSyncTimestamp,
LastUpdateHash: mustSyncReq.LatestHash,
LastSyncTimestamp: mustSyncReq.LatestSyncTimestamp,
LastSyncHash: mustSyncReq.LatestHash,
}
badger.Update(path, updatedSyncMeta.Encode())
}
err = qclient.GiveYouHandler(stream, UUID, afterPath, mustSyncReq.LatestSyncTimestamp, mustSyncReq.LatestHash)
if err != nil {
return err
}
return nil
})
if err != nil {
log.Println("quics-client : [MUSTSYNC] ", err)
}
}
// CheckCanOverwrite checks whether the file can be overwritten
func CheckCanOverwrite(LastSyncTimestamp uint64, LastUpdateTimestamp uint64, LastestSyncTimestamp uint64) bool {
if LastSyncTimestamp == LastUpdateTimestamp && LastestSyncTimestamp > LastSyncTimestamp {
return true
}
return false
}