Himabindu Pucha | fb26a83 | 2015-05-20 15:37:50 -0700 | [diff] [blame] | 1 | // 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 | |
| 5 | package vsync |
| 6 | |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 7 | import ( |
Adam Sadovsky | f2efeb5 | 2015-08-31 14:17:49 -0700 | [diff] [blame] | 8 | "v.io/x/ref/services/syncbase/server/interfaces" |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 9 | ) |
| 10 | |
Himabindu Pucha | 0ff9b0c | 2015-07-14 11:40:45 -0700 | [diff] [blame] | 11 | // Key prefixes for sync data structures. All these prefixes are prepended with |
| 12 | // util.SyncPrefix. |
| 13 | const ( |
Himabindu Pucha | b41fc14 | 2015-09-10 17:10:57 -0700 | [diff] [blame] | 14 | 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 Pucha | 0ff9b0c | 2015-07-14 11:40:45 -0700 | [diff] [blame] | 19 | ) |
| 20 | |
Himabindu Pucha | fb26a83 | 2015-05-20 15:37:50 -0700 | [diff] [blame] | 21 | // syncData represents the persistent state of the sync module. |
| 22 | type syncData struct { |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 23 | Id uint64 |
| 24 | } |
| 25 | |
Himabindu Pucha | b41fc14 | 2015-09-10 17:10:57 -0700 | [diff] [blame] | 26 | // localGenInfo represents the persistent state corresponding to local generations. |
| 27 | type 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 Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 32 | // dbSyncState represents the persistent sync state of a Database. |
| 33 | type dbSyncState struct { |
Himabindu Pucha | b41fc14 | 2015-09-10 17:10:57 -0700 | [diff] [blame] | 34 | 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 Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Himabindu Pucha | 5349702 | 2015-06-19 11:57:13 -0700 | [diff] [blame] | 40 | // localLogRec represents the persistent local state of a log record. Metadata |
| 41 | // is synced across peers, while pos is local-only. |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 42 | type localLogRec struct { |
| 43 | Metadata interfaces.LogRecMetadata |
| 44 | Pos uint64 // position in the Database log. |
Himabindu Pucha | fb26a83 | 2015-05-20 15:37:50 -0700 | [diff] [blame] | 45 | } |
Raja Daoud | 5285136 | 2015-09-14 15:50:40 -0700 | [diff] [blame] | 46 | |
| 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. |
| 50 | type 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 Daoud | 7f029e1 | 2015-09-16 14:27:25 -0700 | [diff] [blame] | 87 | |
| 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. |
| 92 | type 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. |
| 107 | type batchInfo struct { |
| 108 | Objects map[string]string |
| 109 | Count uint64 |
| 110 | } |