blob: 820dec6d23b820eef013a720e438109610be7278 [file] [log] [blame]
Himabindu Puchafb26a832015-05-20 15:37:50 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package vsync
6
Himabindu Pucha2752a7e2015-06-12 14:07:07 -07007import (
Adam Sadovskyf2efeb52015-08-31 14:17:49 -07008 "v.io/x/ref/services/syncbase/server/interfaces"
Himabindu Pucha2752a7e2015-06-12 14:07:07 -07009)
10
Himabindu Pucha0ff9b0c2015-07-14 11:40:45 -070011// Key prefixes for sync data structures. All these prefixes are prepended with
12// util.SyncPrefix.
13const (
Himabindu Puchab41fc142015-09-10 17:10:57 -070014 logPrefix = "log" // log state.
15 logDataPrefix = "data" // data log state.
16 dbssPrefix = "dbss" // database sync state.
17 dagPrefix = "dag" // dag state.
18 sgPrefix = "sg" // syncgroup state.
Himabindu Pucha0ff9b0c2015-07-14 11:40:45 -070019)
20
Himabindu Puchafb26a832015-05-20 15:37:50 -070021// syncData represents the persistent state of the sync module.
22type syncData struct {
Himabindu Pucha2752a7e2015-06-12 14:07:07 -070023 Id uint64
24}
25
Himabindu Puchab41fc142015-09-10 17:10:57 -070026// localGenInfo represents the persistent state corresponding to local generations.
27type localGenInfo struct {
28 Gen uint64 // local generation number incremented on every local update.
29 CheckptGen uint64 // local generation number advertised to remote peers (used by the responder).
30}
31
Himabindu Pucha2752a7e2015-06-12 14:07:07 -070032// dbSyncState represents the persistent sync state of a Database.
33type dbSyncState struct {
Himabindu Puchab41fc142015-09-10 17:10:57 -070034 Data localGenInfo
35 Sgs map[interfaces.GroupId]localGenInfo
36 GenVec interfaces.GenVector // generation vector capturing the locally-known generations of remote peers for data in Database.
37 SgGenVec interfaces.GenVector // generation vector capturing the locally-known generations of remote peers for SyncGroups in Database.
Himabindu Pucha2752a7e2015-06-12 14:07:07 -070038}
39
Himabindu Pucha53497022015-06-19 11:57:13 -070040// localLogRec represents the persistent local state of a log record. Metadata
41// is synced across peers, while pos is local-only.
Himabindu Pucha2752a7e2015-06-12 14:07:07 -070042type localLogRec struct {
43 Metadata interfaces.LogRecMetadata
44 Pos uint64 // position in the Database log.
Himabindu Puchafb26a832015-05-20 15:37:50 -070045}
Raja Daoud52851362015-09-14 15:50:40 -070046
47// sgLocalState holds the SyncGroup local state, only relevant to this member
48// (i.e. the local Syncbase). This is needed for crash recovery of the internal
49// state transitions of the SyncGroup.
50type sgLocalState struct {
51 // The count of local joiners to the same SyncGroup.
52 NumLocalJoiners uint32
53
54 // The SyncGroup is watched when the sync Watcher starts processing the
55 // SyncGroup data. When a SyncGroup is created or joined, an entry is
56 // added to the Watcher queue (log) to inform it from which point to
57 // start accepting store mutations, an asynchronous notification similar
58 // to regular store mutations. When the Watcher processes that queue
59 // entry, it sets this bit to true. When Syncbase restarts, the value
60 // of this bit allows the new sync Watcher to recreate its in-memory
61 // state by resuming to watch only the prefixes of SyncGroups that were
62 // previously being watched.
63 Watched bool
64
65 // The SyncGroup was published here by this remote peer (if non-empty
66 // string), typically the SyncGroup creator. In this case the SyncGroup
67 // cannot be GCed locally even if it has no local joiners.
68 RemotePublisher string
69
70 // The SyncGroup is in pending state on a device that learns the current
71 // state of the SyncGroup from another device but has not yet received
72 // through peer-to-peer sync the history of the changes (DAG and logs).
73 // This happens in two cases:
74 // 1- A joiner was accepted into a SyncGroup by a SyncGroup admin and
75 // only given the current SyncGroup info synchronously and will
76 // receive the full history later via p2p sync.
77 // 2- A remote server where the SyncGroup is published was told by the
78 // SyncGroup publisher the current SyncGroup info synchronously and
79 // will receive the full history later via p2p sync.
80 // The pending state is over when the device reaches or exceeds the
81 // knowledge level indicated in the pending genvec. While SyncPending
82 // is true, no local SyncGroup mutations are allowed (i.e. no join or
83 // set-spec requests).
84 SyncPending bool
85 PendingGenVec interfaces.PrefixGenVector
86}
Raja Daoud7f029e12015-09-16 14:27:25 -070087
88// dagNode holds the information on an object mutation in the DAG. The node
89// information is extracted from the log records exchanged between Syncbases.
90// They are also stored in the DAG node to improve DAG traversal for conflict
91// resolution and pruning without having to fetch the full log record.
92type dagNode struct {
93 Level uint64 // node distance from root
94 Parents []string // references to parent versions
95 Logrec string // reference to log record
96 BatchId uint64 // ID of a write batch
97 Shell bool // true when the data is hidden due to permissions
98 Deleted bool // true if the change was a delete
99 PermId string // ID of the permissions controlling this version
100 PermVers string // current version of the permissions object
101}
102
103// batchInfo holds the information on a write batch:
104// - The map of syncable (versioned) objects: {oid: version}
105// - The total count of batch objects, including non-syncable ones.
106// TODO(rdaoud): add support to track the read and scan sets.
107type batchInfo struct {
108 Objects map[string]string
109 Count uint64
110}