Adam Sadovsky | b85e353 | 2015-04-08 20:38:27 -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 | |
| 7 | import ( |
| 8 | "v.io/v23/security/access" |
| 9 | ) |
| 10 | |
| 11 | // temporary types |
| 12 | type ObjId string |
| 13 | type Version uint64 |
| 14 | type GroupId uint64 |
| 15 | |
| 16 | // DeviceId is the globally unique Id of a device. |
| 17 | type DeviceId string |
| 18 | // GenId is the unique Id per generation per device. |
| 19 | type GenId uint64 |
| 20 | // SeqNum is the log sequence number. |
| 21 | type SeqNum uint64 |
| 22 | // GenVector is the generation vector. |
| 23 | type GenVector map[DeviceId]GenId |
| 24 | // TxId is the unique Id per transaction. |
| 25 | type TxId uint64 |
| 26 | // GroupIdSet is the list of SyncGroup Ids. |
| 27 | type GroupIdSet []GroupId |
| 28 | |
| 29 | const ( |
| 30 | // NodeRec type log record adds a new node in the dag. |
| 31 | NodeRec = byte(0) |
| 32 | // LinkRec type log record adds a new link in the dag. |
| 33 | LinkRec = byte(1) |
| 34 | |
| 35 | // Sync interface has Object name "global/vsync/<devid>/sync". |
| 36 | SyncSuffix = "sync" |
| 37 | |
| 38 | // temporary nil values |
| 39 | NoObjId = ObjId("") |
| 40 | NoVersion = Version(0) |
| 41 | NoGroupId = GroupId(0) |
| 42 | ) |
| 43 | |
| 44 | // LogRec represents a single log record that is exchanged between two |
| 45 | // peers. |
| 46 | // |
| 47 | // It contains log related metadata: DevId is the id of the device |
| 48 | // that created the log record, SyncRootId is the id of a SyncRoot this log |
| 49 | // record was created under, GNum is the Id of the generation that the |
| 50 | // log record is part of, SeqNum is the log sequence number of the log |
| 51 | // record in the generation GNum, and RecType is the type of log |
| 52 | // record. |
| 53 | // |
| 54 | // It also contains information relevant to the updates to an object |
| 55 | // in the store: ObjId is the id of the object that was |
| 56 | // updated. CurVers is the current version number of the |
| 57 | // object. Parents can contain 0, 1 or 2 parent versions that the |
| 58 | // current version is derived from, and Value is the actual value of |
| 59 | // the object mutation. |
| 60 | type LogRec struct { |
| 61 | // Log related information. |
| 62 | DevId DeviceId |
| 63 | SyncRootId ObjId |
| 64 | GenNum GenId |
| 65 | SeqNum SeqNum |
| 66 | RecType byte |
| 67 | |
| 68 | // Object related information. |
| 69 | ObjId ObjId |
| 70 | CurVers Version |
| 71 | Parents []Version |
| 72 | Value LogValue |
| 73 | } |
| 74 | |
| 75 | // LogValue represents an object mutation within a transaction. |
| 76 | type LogValue struct { |
| 77 | // Mutation is the store mutation representing the change in the object. |
| 78 | //Mutation raw.Mutation |
| 79 | // SyncTime is the timestamp of the mutation when it arrives at the Sync server. |
| 80 | SyncTime int64 |
| 81 | // Delete indicates whether the mutation resulted in the object being |
| 82 | // deleted from the store. |
| 83 | Delete bool |
| 84 | // TxId is the unique Id of the transaction this mutation belongs to. |
| 85 | TxId TxId |
| 86 | // TxCount is the number of mutations in the transaction TxId. |
| 87 | TxCount uint32 |
| 88 | } |
| 89 | |
| 90 | // DeviceStats contains high-level information on a device participating in |
| 91 | // peer-to-peer synchronization. |
| 92 | type DeviceStats struct { |
| 93 | DevId DeviceId // Device Id. |
| 94 | LastSync int64 // Timestamp of last sync from the device. |
| 95 | GenVectors map[ObjId]GenVector // Generation vectors per SyncRoot. |
| 96 | IsSelf bool // True if the responder is on this device. |
| 97 | } |
| 98 | |
| 99 | // SyncGroupStats contains high-level information on a SyncGroup. |
| 100 | type SyncGroupStats struct { |
| 101 | Name string // Global name of the SyncGroup. |
| 102 | Id GroupId // Global Id of the SyncGroup. |
| 103 | Path string // Local store path for the root of the SyncGroup. |
| 104 | RootObjId ObjId // Id of the store object at the root path. |
| 105 | NumJoiners uint32 // Number of members currently in the SyncGroup. |
| 106 | } |
| 107 | |
| 108 | // SyncGroupMember contains information on a SyncGroup member. |
| 109 | type SyncGroupMember struct { |
| 110 | Name string // Name of SyncGroup member. |
| 111 | Id GroupId // Global Id of the SyncGroup. |
| 112 | Metadata JoinerMetaData // Extra member metadata. |
| 113 | } |
| 114 | |
| 115 | // A SyncGroupInfo is the conceptual state of a SyncGroup object. |
| 116 | type SyncGroupInfo struct { |
| 117 | Id GroupId // Globally unique SyncGroup Id. |
| 118 | ServerName string // Global Vanadium name of SyncGroupServer. |
| 119 | GroupName string // Relative name of group; global name is ServerName/GroupName. |
| 120 | Config SyncGroupConfig // Configuration parameters of this SyncGroup. |
| 121 | ETag string // Version Id for concurrency control. |
| 122 | |
| 123 | // A map from joiner names to the associated metaData for devices that |
| 124 | // have called Join() or Create() and not subsequently called Leave() |
| 125 | // or had Eject() called on them. The map returned by the calls below |
| 126 | // may contain only a subset of joiners if the number is large. |
| 127 | Joiners map[string]JoinerMetaData |
| 128 | |
| 129 | // Blessings for joiners of this SyncGroup will be self-signed by the |
| 130 | // SyncGroupServer, and will have names matching |
| 131 | // JoinerBlessingPrefix/Name/... |
| 132 | JoinerBlessingPrefix string |
| 133 | } |
| 134 | |
| 135 | // A SyncGroupConfig contains some fields of SyncGroupInfo that |
| 136 | // are passed at create time, but which can be changed later. |
| 137 | type SyncGroupConfig struct { |
| 138 | Desc string // Human readable description. |
| 139 | Options map[string]any // Options for future evolution. |
| 140 | Permissions access.Permissions // The object's Permissions. |
| 141 | |
| 142 | // Mount tables used to advertise for synchronization. |
| 143 | // Typically, we will have only one entry. However, an array allows |
| 144 | // mount tables to be changed over time. |
| 145 | MountTables []string |
| 146 | |
| 147 | BlessingsDurationNanos int64 // Duration of blessings, in nanoseconds. 0 => use server default. |
| 148 | } |
| 149 | |
| 150 | // A JoinerMetaData contains the non-name information stored per joiner. |
| 151 | type JoinerMetaData struct { |
| 152 | // SyncPriority is a hint to bias the choice of syncing partners. |
| 153 | // Members of the SyncGroup should choose to synchronize more often |
| 154 | // with partners with lower values. |
| 155 | SyncPriority int32 |
| 156 | } |
| 157 | |
| 158 | // Sync allows a device to GetDeltas from another device. |
| 159 | type Sync interface { |
| 160 | // GetDeltas returns a device's current generation vector and all |
| 161 | // the missing log records when compared to the incoming generation vector. |
| 162 | GetDeltas(in map[ObjId]GenVector, sgs map[ObjId]GroupIdSet, clientId DeviceId) stream<_, LogRec> (map[ObjId]GenVector | error) {access.Write} |
| 163 | |
| 164 | // GetObjectHistory returns the mutation history of a store object. |
| 165 | GetObjectHistory(oid ObjId) stream<_, LogRec> (Version | error) |
| 166 | |
| 167 | // GetDeviceStats returns information on devices participating in |
| 168 | // peer-to-peer synchronization. |
| 169 | GetDeviceStats() stream<_, DeviceStats> error {access.Admin} |
| 170 | |
| 171 | // GetSyncGroupMembers returns information on SyncGroup members. |
| 172 | // If SyncGroup names are specified, only members of these SyncGroups |
| 173 | // are returned. Otherwise, if the slice of names is nil or empty, |
| 174 | // members of all SyncGroups are returned. |
| 175 | GetSyncGroupMembers(sgNames []string) stream<_, SyncGroupMember> error {access.Admin} |
| 176 | |
| 177 | // GetSyncGroupStats returns high-level information on all SyncGroups. |
| 178 | GetSyncGroupStats() stream<_, SyncGroupStats> error {access.Admin} |
| 179 | |
| 180 | // Dump writes to the Sync log internal information used for debugging. |
| 181 | Dump() error {access.Admin} |
| 182 | } |