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 | |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 7 | // Syncbase Watcher is a goroutine that listens to local Database updates from |
| 8 | // applications and modifies sync metadata (e.g. DAG and local log records). |
| 9 | // The coupling between Syncbase storage and sync is loose, via asynchronous |
| 10 | // listening by the Watcher, to unblock the application operations as soon as |
| 11 | // possible, and offload the sync metadata update to the Watcher. When the |
| 12 | // application mutates objects in a Database, additional entries are written |
| 13 | // to a log queue, persisted in the same Database. This queue is read by the |
| 14 | // sync Watcher to learn of the changes. |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 15 | |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 16 | import ( |
| 17 | "strings" |
| 18 | "time" |
| 19 | |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 20 | "v.io/v23/context" |
Sergey Rogulenko | 40402b5 | 2015-08-10 15:09:48 -0700 | [diff] [blame] | 21 | "v.io/v23/services/watch" |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 22 | "v.io/v23/verror" |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 23 | "v.io/x/lib/vlog" |
Adam Sadovsky | f2efeb5 | 2015-08-31 14:17:49 -0700 | [diff] [blame] | 24 | "v.io/x/ref/services/syncbase/server/interfaces" |
| 25 | "v.io/x/ref/services/syncbase/server/util" |
| 26 | "v.io/x/ref/services/syncbase/server/watchable" |
| 27 | "v.io/x/ref/services/syncbase/store" |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 30 | var ( |
Adam Sadovsky | ebc183a | 2015-10-08 22:26:47 -0700 | [diff] [blame] | 31 | // watchPrefixes is an in-memory cache of syncgroup prefixes for each |
| 32 | // app database. It is filled at startup from persisted syncgroup data |
| 33 | // and updated at runtime when syncgroups are joined or left. It is |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 34 | // not guarded by a mutex because only the watcher goroutine uses it |
| 35 | // beyond the startup phase (before any sync goroutines are started). |
| 36 | // The map keys are the appdb names (globally unique). |
| 37 | watchPrefixes = make(map[string]sgPrefixes) |
| 38 | ) |
| 39 | |
Adam Sadovsky | ebc183a | 2015-10-08 22:26:47 -0700 | [diff] [blame] | 40 | // sgPrefixes tracks syncgroup prefixes being synced in a database and their |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 41 | // syncgroups. |
| 42 | type sgPrefixes map[string]sgSet |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 43 | |
| 44 | // watchStore processes updates obtained by watching the store. This is the |
| 45 | // sync watcher goroutine that learns about store updates asynchronously by |
| 46 | // reading log records that track object mutation histories in each database. |
| 47 | // For each batch mutation, the watcher updates the sync DAG and log records. |
| 48 | // When an application makes a single non-transactional put, it is represented |
| 49 | // as a batch of one log record. Thus the watcher only deals with batches. |
| 50 | func (s *syncService) watchStore(ctx *context.T) { |
Himabindu Pucha | fb26a83 | 2015-05-20 15:37:50 -0700 | [diff] [blame] | 51 | defer s.pending.Done() |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 52 | |
| 53 | ticker := time.NewTicker(watchPollInterval) |
| 54 | defer ticker.Stop() |
| 55 | |
| 56 | ctx, cancel := context.WithCancel(ctx) |
| 57 | defer cancel() |
| 58 | |
Raja Daoud | c0a5076 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 59 | for !s.Closed() { |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 60 | select { |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 61 | case <-ticker.C: |
Raja Daoud | c0a5076 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 62 | if s.Closed() { |
| 63 | break |
| 64 | } |
| 65 | s.processStoreUpdates(ctx) |
Himabindu Pucha | 7cee181 | 2015-10-06 22:21:55 -0700 | [diff] [blame] | 66 | |
Himabindu Pucha | 7cee181 | 2015-10-06 22:21:55 -0700 | [diff] [blame] | 67 | case <-s.closed: |
Raja Daoud | c0a5076 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 68 | break |
Himabindu Pucha | 7cee181 | 2015-10-06 22:21:55 -0700 | [diff] [blame] | 69 | } |
Raja Daoud | f510458 | 2015-07-31 17:16:21 -0700 | [diff] [blame] | 70 | } |
Raja Daoud | c0a5076 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 71 | |
| 72 | vlog.VI(1).Info("sync: watchStore: channel closed, stop watching and exit") |
Raja Daoud | f510458 | 2015-07-31 17:16:21 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // processStoreUpdates fetches updates from all databases and processes them. |
| 76 | // To maintain fairness among databases, it processes one batch update from |
| 77 | // each database, in a round-robin manner, until there are no further updates |
| 78 | // from any database. |
| 79 | func (s *syncService) processStoreUpdates(ctx *context.T) { |
| 80 | for { |
| 81 | total, active := 0, 0 |
| 82 | s.forEachDatabaseStore(ctx, func(appName, dbName string, st store.Store) bool { |
| 83 | if s.processDatabase(ctx, appName, dbName, st) { |
| 84 | active++ |
| 85 | } |
| 86 | total++ |
| 87 | return false |
| 88 | }) |
| 89 | |
| 90 | vlog.VI(2).Infof("sync: processStoreUpdates: %d/%d databases had updates", active, total) |
| 91 | if active == 0 { |
| 92 | break |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
Himabindu Pucha | fb26a83 | 2015-05-20 15:37:50 -0700 | [diff] [blame] | 95 | } |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 96 | |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 97 | // processDatabase fetches from the given database at most one new batch update |
| 98 | // (transaction) and processes it. The one-batch limit prevents one database |
| 99 | // from starving others. A batch is stored as a contiguous set of log records |
Raja Daoud | f510458 | 2015-07-31 17:16:21 -0700 | [diff] [blame] | 100 | // ending with one record having the "continued" flag set to false. The call |
| 101 | // returns true if a new batch update was processed. |
| 102 | func (s *syncService) processDatabase(ctx *context.T, appName, dbName string, st store.Store) bool { |
Himabindu Pucha | 05358cb | 2015-07-28 08:49:48 -0700 | [diff] [blame] | 103 | s.thLock.Lock() |
| 104 | defer s.thLock.Unlock() |
| 105 | |
Raja Daoud | 4171c9c | 2015-07-14 20:07:44 -0700 | [diff] [blame] | 106 | vlog.VI(2).Infof("sync: processDatabase: begin: %s, %s", appName, dbName) |
| 107 | defer vlog.VI(2).Infof("sync: processDatabase: end: %s, %s", appName, dbName) |
| 108 | |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 109 | resMark, err := getResMark(ctx, st) |
| 110 | if err != nil { |
| 111 | if verror.ErrorID(err) != verror.ErrNoExist.ID { |
Raja Daoud | 4171c9c | 2015-07-14 20:07:44 -0700 | [diff] [blame] | 112 | vlog.Errorf("sync: processDatabase: %s, %s: cannot get resMark: %v", appName, dbName, err) |
Raja Daoud | f510458 | 2015-07-31 17:16:21 -0700 | [diff] [blame] | 113 | return false |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 114 | } |
Sergey Rogulenko | 40402b5 | 2015-08-10 15:09:48 -0700 | [diff] [blame] | 115 | resMark = watchable.MakeResumeMarker(0) |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Raja Daoud | 7cb7179 | 2015-07-08 12:00:33 -0700 | [diff] [blame] | 118 | // Initialize Database sync state if needed. |
Himabindu Pucha | f2796e1 | 2015-10-01 19:05:10 -0700 | [diff] [blame] | 119 | s.initSyncStateInMem(ctx, appName, dbName, "") |
Raja Daoud | 7cb7179 | 2015-07-08 12:00:33 -0700 | [diff] [blame] | 120 | |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 121 | // Get a batch of watch log entries, if any, after this resume marker. |
Sergey Rogulenko | 8bf641c | 2015-08-14 17:00:09 -0700 | [diff] [blame] | 122 | logs, nextResmark, err := watchable.ReadBatchFromLog(st, resMark) |
Sergey Rogulenko | 40402b5 | 2015-08-10 15:09:48 -0700 | [diff] [blame] | 123 | if err != nil { |
| 124 | vlog.Fatalf("sync: processDatabase: %s, %s: cannot get watch log batch: %v", appName, dbName, verror.DebugString(err)) |
| 125 | } |
| 126 | if logs != nil { |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 127 | s.processWatchLogBatch(ctx, appName, dbName, st, logs, nextResmark) |
Raja Daoud | f510458 | 2015-07-31 17:16:21 -0700 | [diff] [blame] | 128 | return true |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 129 | } |
Raja Daoud | f510458 | 2015-07-31 17:16:21 -0700 | [diff] [blame] | 130 | return false |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // processWatchLogBatch parses the given batch of watch log records, updates the |
Adam Sadovsky | ebc183a | 2015-10-08 22:26:47 -0700 | [diff] [blame] | 134 | // watchable syncgroup prefixes, uses the prefixes to filter the batch to the |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 135 | // subset of syncable records, and transactionally applies these updates to the |
| 136 | // sync metadata (DAG & log records) and updates the watch resume marker. |
Sergey Rogulenko | 40402b5 | 2015-08-10 15:09:48 -0700 | [diff] [blame] | 137 | func (s *syncService) processWatchLogBatch(ctx *context.T, appName, dbName string, st store.Store, logs []*watchable.LogEntry, resMark watch.ResumeMarker) { |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 138 | if len(logs) == 0 { |
| 139 | return |
| 140 | } |
| 141 | |
Adam Sadovsky | ebc183a | 2015-10-08 22:26:47 -0700 | [diff] [blame] | 142 | // If the first log entry is a syncgroup prefix operation, then this is |
| 143 | // a syncgroup snapshot and not an application batch. In this case, |
| 144 | // handle the syncgroup prefix changes by updating the watch prefixes |
Raja Daoud | 0dfdd25 | 2015-07-10 20:02:22 -0700 | [diff] [blame] | 145 | // and exclude the first entry from the batch. Also inform the batch |
| 146 | // processing below to not assign it a batch ID in the DAG. |
| 147 | appBatch := true |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 148 | sgop := processSyncgroupLogRecord(appName, dbName, logs[0]) |
| 149 | if sgop != nil { |
Raja Daoud | 0dfdd25 | 2015-07-10 20:02:22 -0700 | [diff] [blame] | 150 | appBatch = false |
| 151 | logs = logs[1:] |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Adam Sadovsky | ebc183a | 2015-10-08 22:26:47 -0700 | [diff] [blame] | 154 | // Filter out the log entries for keys not part of any syncgroup. |
Raja Daoud | d454307 | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 155 | // Ignore as well log entries made by sync (echo suppression). |
Raja Daoud | 0dfdd25 | 2015-07-10 20:02:22 -0700 | [diff] [blame] | 156 | totalCount := uint64(len(logs)) |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 157 | appdb := appDbName(appName, dbName) |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 158 | |
Raja Daoud | 0dfdd25 | 2015-07-10 20:02:22 -0700 | [diff] [blame] | 159 | i := 0 |
| 160 | for _, entry := range logs { |
Raja Daoud | d454307 | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 161 | if !entry.FromSync && syncable(appdb, entry) { |
Raja Daoud | 0dfdd25 | 2015-07-10 20:02:22 -0700 | [diff] [blame] | 162 | logs[i] = entry |
| 163 | i++ |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
Raja Daoud | 0dfdd25 | 2015-07-10 20:02:22 -0700 | [diff] [blame] | 166 | logs = logs[:i] |
Raja Daoud | 4171c9c | 2015-07-14 20:07:44 -0700 | [diff] [blame] | 167 | vlog.VI(3).Infof("sync: processWatchLogBatch: %s, %s: sg snap %t, syncable %d, total %d", |
| 168 | appName, dbName, !appBatch, len(logs), totalCount) |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 169 | |
| 170 | // Transactional processing of the batch: convert these syncable log |
| 171 | // records to a batch of sync log records, filling their parent versions |
| 172 | // from the DAG head nodes. |
Mike Burrows | 45573ce | 2015-11-19 18:02:12 -0800 | [diff] [blame] | 173 | batch := make([]*LocalLogRec, len(logs)) |
Sergey Rogulenko | 1068b1a | 2015-08-03 16:53:27 -0700 | [diff] [blame] | 174 | err := store.RunInTransaction(st, func(tx store.Transaction) error { |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 175 | i := 0 |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 176 | for _, entry := range logs { |
Raja Daoud | 4171c9c | 2015-07-14 20:07:44 -0700 | [diff] [blame] | 177 | if rec, err := convertLogRecord(ctx, tx, entry); err != nil { |
| 178 | return err |
| 179 | } else if rec != nil { |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 180 | batch[i] = rec |
| 181 | i++ |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 184 | batch = batch[:i] |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 185 | |
Raja Daoud | 0dfdd25 | 2015-07-10 20:02:22 -0700 | [diff] [blame] | 186 | if err := s.processBatch(ctx, appName, dbName, batch, appBatch, totalCount, tx); err != nil { |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 187 | return err |
| 188 | } |
Himabindu Pucha | 7cee181 | 2015-10-06 22:21:55 -0700 | [diff] [blame] | 189 | |
| 190 | if !appBatch { |
Adam Sadovsky | ebc183a | 2015-10-08 22:26:47 -0700 | [diff] [blame] | 191 | if err := setSyncgroupWatchable(ctx, tx, sgop); err != nil { |
Himabindu Pucha | 7cee181 | 2015-10-06 22:21:55 -0700 | [diff] [blame] | 192 | return err |
| 193 | } |
| 194 | } |
| 195 | |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 196 | return setResMark(ctx, tx, resMark) |
| 197 | }) |
| 198 | |
| 199 | if err != nil { |
| 200 | // TODO(rdaoud): don't crash, quarantine this app database. |
Adam Sadovsky | ad4857e | 2015-10-26 14:54:45 -0700 | [diff] [blame] | 201 | vlog.Fatalf("sync: processWatchLogBatch: %s, %s: watcher cannot process batch: %v", appName, dbName, err) |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 202 | } |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 203 | |
| 204 | // Extract blob refs from batch values and update blob metadata. |
| 205 | // TODO(rdaoud): the core of this step, extracting blob refs from the |
| 206 | // app data, is idempotent and should be done before the transaction |
| 207 | // above, which updated the resume marker. If Syncbase crashes here it |
| 208 | // does not re-do this blob ref processing at restart and the metadata |
| 209 | // becomes lower quality. Unfortunately, the log conversion must happen |
| 210 | // inside the transaction because it accesses DAG information that must |
| 211 | // be in the Tx read-set for optimistic locking. The TODO is to split |
| 212 | // the conversion into two phases, one non-transactional that happens |
| 213 | // first outside the transaction, followed by blob ref processing also |
| 214 | // outside the transaction (idempotent), then inside the transaction |
| 215 | // patch-up the log records in a 2nd phase. |
| 216 | if err = s.processWatchBlobRefs(ctx, appdb, st, batch); err != nil { |
| 217 | vlog.Fatalf("sync: processWatchLogBatch:: %s, %s: watcher cannot process blob refs: %v", appName, dbName, err) |
| 218 | } |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 219 | } |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 220 | |
| 221 | // processBatch applies a single batch of changes (object mutations) received |
| 222 | // from watching a particular Database. |
Mike Burrows | 45573ce | 2015-11-19 18:02:12 -0800 | [diff] [blame] | 223 | func (s *syncService) processBatch(ctx *context.T, appName, dbName string, batch []*LocalLogRec, appBatch bool, totalCount uint64, tx store.Transaction) error { |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 224 | count := uint64(len(batch)) |
| 225 | if count == 0 { |
| 226 | return nil |
| 227 | } |
| 228 | |
Raja Daoud | 0dfdd25 | 2015-07-10 20:02:22 -0700 | [diff] [blame] | 229 | // If an application batch has more than one mutation, start a batch for it. |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 230 | batchId := NoBatchId |
Raja Daoud | 0dfdd25 | 2015-07-10 20:02:22 -0700 | [diff] [blame] | 231 | if appBatch && totalCount > 1 { |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 232 | batchId = s.startBatch(ctx, tx, batchId) |
| 233 | if batchId == NoBatchId { |
| 234 | return verror.New(verror.ErrInternal, ctx, "failed to generate batch ID") |
| 235 | } |
| 236 | } |
| 237 | |
Himabindu Pucha | b41fc14 | 2015-09-10 17:10:57 -0700 | [diff] [blame] | 238 | gen, pos := s.reserveGenAndPosInDbLog(ctx, appName, dbName, "", count) |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 239 | |
Raja Daoud | 4171c9c | 2015-07-14 20:07:44 -0700 | [diff] [blame] | 240 | vlog.VI(3).Infof("sync: processBatch: %s, %s: len %d, total %d, btid %x, gen %d, pos %d", |
| 241 | appName, dbName, count, totalCount, batchId, gen, pos) |
| 242 | |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 243 | for _, rec := range batch { |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 244 | // Update the log record. Portions of the record Metadata must |
| 245 | // already be filled. |
| 246 | rec.Metadata.Id = s.id |
| 247 | rec.Metadata.Gen = gen |
| 248 | rec.Metadata.RecType = interfaces.NodeRec |
| 249 | |
| 250 | rec.Metadata.BatchId = batchId |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 251 | rec.Metadata.BatchCount = totalCount |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 252 | |
| 253 | rec.Pos = pos |
| 254 | |
| 255 | gen++ |
| 256 | pos++ |
| 257 | |
| 258 | if err := s.processLocalLogRec(ctx, tx, rec); err != nil { |
| 259 | return verror.New(verror.ErrInternal, ctx, err) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // End the batch if any. |
| 264 | if batchId != NoBatchId { |
Raja Daoud | 0dfdd25 | 2015-07-10 20:02:22 -0700 | [diff] [blame] | 265 | if err := s.endBatch(ctx, tx, batchId, totalCount); err != nil { |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 266 | return err |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | return nil |
| 271 | } |
| 272 | |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 273 | // processLocalLogRec processes a local log record by adding to the Database and |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 274 | // suitably updating the DAG metadata. |
Mike Burrows | 45573ce | 2015-11-19 18:02:12 -0800 | [diff] [blame] | 275 | func (s *syncService) processLocalLogRec(ctx *context.T, tx store.Transaction, rec *LocalLogRec) error { |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 276 | // Insert the new log record into the log. |
Himabindu Pucha | b41fc14 | 2015-09-10 17:10:57 -0700 | [diff] [blame] | 277 | if err := putLogRec(ctx, tx, logDataPrefix, rec); err != nil { |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 278 | return err |
| 279 | } |
| 280 | |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 281 | m := &rec.Metadata |
Himabindu Pucha | b41fc14 | 2015-09-10 17:10:57 -0700 | [diff] [blame] | 282 | logKey := logRecKey(logDataPrefix, m.Id, m.Gen) |
Himabindu Pucha | 2752a7e | 2015-06-12 14:07:07 -0700 | [diff] [blame] | 283 | |
| 284 | // Insert the new log record into dag. |
| 285 | if err := s.addNode(ctx, tx, m.ObjId, m.CurVers, logKey, m.Delete, m.Parents, m.BatchId, nil); err != nil { |
| 286 | return err |
| 287 | } |
| 288 | |
| 289 | // Move the head. |
| 290 | return moveHead(ctx, tx, m.ObjId, m.CurVers) |
| 291 | } |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 292 | |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 293 | // processWatchBlobRefs extracts blob refs from the data values of the updates |
| 294 | // received in the watch batch and updates the blob-to-syncgroup metadata. |
Mike Burrows | 45573ce | 2015-11-19 18:02:12 -0800 | [diff] [blame] | 295 | func (s *syncService) processWatchBlobRefs(ctx *context.T, appdb string, st store.Store, batch []*LocalLogRec) error { |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 296 | if len(batch) == 0 { |
| 297 | return nil |
| 298 | } |
| 299 | |
| 300 | sgPfxs := watchPrefixes[appdb] |
| 301 | if len(sgPfxs) == 0 { |
| 302 | return verror.New(verror.ErrInternal, ctx, "processWatchBlobRefs: no sg prefixes in db", appdb) |
| 303 | } |
| 304 | |
| 305 | for _, rec := range batch { |
| 306 | m := &rec.Metadata |
| 307 | if m.Delete { |
| 308 | continue |
| 309 | } |
| 310 | |
| 311 | buf, err := watchable.GetAtVersion(ctx, st, []byte(m.ObjId), nil, []byte(m.CurVers)) |
| 312 | if err != nil { |
| 313 | return err |
| 314 | } |
| 315 | |
| 316 | if err = s.processBlobRefs(ctx, s.name, sgPfxs, m, buf); err != nil { |
| 317 | return err |
| 318 | } |
| 319 | } |
| 320 | return nil |
| 321 | } |
| 322 | |
| 323 | // addWatchPrefixSyncgroup adds a syncgroup prefix-to-ID mapping for an app |
| 324 | // database in the watch prefix cache. |
| 325 | func addWatchPrefixSyncgroup(appName, dbName, prefix string, gid interfaces.GroupId) { |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 326 | name := appDbName(appName, dbName) |
| 327 | if pfxs := watchPrefixes[name]; pfxs != nil { |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 328 | if sgs := pfxs[prefix]; sgs != nil { |
| 329 | sgs[gid] = struct{}{} |
| 330 | } else { |
| 331 | pfxs[prefix] = sgSet{gid: struct{}{}} |
| 332 | } |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 333 | } else { |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 334 | watchPrefixes[name] = sgPrefixes{prefix: sgSet{gid: struct{}{}}} |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 338 | // rmWatchPrefixSyncgroup removes a syncgroup prefix-to-ID mapping for an app |
| 339 | // database in the watch prefix cache. |
| 340 | func rmWatchPrefixSyncgroup(appName, dbName, prefix string, gid interfaces.GroupId) { |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 341 | name := appDbName(appName, dbName) |
| 342 | if pfxs := watchPrefixes[name]; pfxs != nil { |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 343 | if sgs := pfxs[prefix]; sgs != nil { |
| 344 | delete(sgs, gid) |
| 345 | if len(sgs) == 0 { |
| 346 | delete(pfxs, prefix) |
| 347 | if len(pfxs) == 0 { |
| 348 | delete(watchPrefixes, name) |
| 349 | } |
| 350 | } |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
Adam Sadovsky | ebc183a | 2015-10-08 22:26:47 -0700 | [diff] [blame] | 355 | // setSyncgroupWatchable sets the local watchable state of the syncgroup. |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 356 | func setSyncgroupWatchable(ctx *context.T, tx store.Transaction, sgop *watchable.SyncgroupOp) error { |
Himabindu Pucha | 7cee181 | 2015-10-06 22:21:55 -0700 | [diff] [blame] | 357 | state, err := getSGIdEntry(ctx, tx, sgop.SgId) |
| 358 | if err != nil { |
| 359 | return err |
| 360 | } |
| 361 | state.Watched = !sgop.Remove |
| 362 | |
| 363 | return setSGIdEntry(ctx, tx, sgop.SgId, state) |
| 364 | } |
| 365 | |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 366 | // convertLogRecord converts a store log entry to a sync log record. It fills |
| 367 | // the previous object version (parent) by fetching its current DAG head if it |
| 368 | // has one. For a delete, it generates a new object version because the store |
| 369 | // does not version a deletion. |
| 370 | // TODO(rdaoud): change Syncbase to store and version a deleted object to |
| 371 | // simplify the store-to-sync interaction. A deleted key would still have a |
| 372 | // version and its value entry would encode the "deleted" flag, either in the |
| 373 | // key or probably in a value wrapper that would contain other metadata. |
Mike Burrows | 45573ce | 2015-11-19 18:02:12 -0800 | [diff] [blame] | 374 | func convertLogRecord(ctx *context.T, tx store.Transaction, logEnt *watchable.LogEntry) (*LocalLogRec, error) { |
| 375 | var rec *LocalLogRec |
Raja Daoud | d454307 | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 376 | timestamp := logEnt.CommitTimestamp |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 377 | |
| 378 | switch op := logEnt.Op.(type) { |
Raja Daoud | 7cb7179 | 2015-07-08 12:00:33 -0700 | [diff] [blame] | 379 | case watchable.OpGet: |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 380 | // TODO(rdaoud): save read-set in sync. |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 381 | |
Raja Daoud | 7cb7179 | 2015-07-08 12:00:33 -0700 | [diff] [blame] | 382 | case watchable.OpScan: |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 383 | // TODO(rdaoud): save scan-set in sync. |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 384 | |
Raja Daoud | 7cb7179 | 2015-07-08 12:00:33 -0700 | [diff] [blame] | 385 | case watchable.OpPut: |
Raja Daoud | d454307 | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 386 | rec = newLocalLogRec(ctx, tx, op.Value.Key, op.Value.Version, false, timestamp) |
| 387 | |
Raja Daoud | 7cb7179 | 2015-07-08 12:00:33 -0700 | [diff] [blame] | 388 | case watchable.OpSyncSnapshot: |
Raja Daoud | 0dfdd25 | 2015-07-10 20:02:22 -0700 | [diff] [blame] | 389 | // Create records for object versions not already in the DAG. |
Adam Sadovsky | ebc183a | 2015-10-08 22:26:47 -0700 | [diff] [blame] | 390 | // Duplicates can appear here in cases of nested syncgroups or |
| 391 | // peer syncgroups. |
Raja Daoud | 4171c9c | 2015-07-14 20:07:44 -0700 | [diff] [blame] | 392 | if ok, err := hasNode(ctx, tx, string(op.Value.Key), string(op.Value.Version)); err != nil { |
| 393 | return nil, err |
| 394 | } else if !ok { |
Raja Daoud | 0dfdd25 | 2015-07-10 20:02:22 -0700 | [diff] [blame] | 395 | rec = newLocalLogRec(ctx, tx, op.Value.Key, op.Value.Version, false, timestamp) |
| 396 | } |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 397 | |
Raja Daoud | 7cb7179 | 2015-07-08 12:00:33 -0700 | [diff] [blame] | 398 | case watchable.OpDelete: |
Raja Daoud | d454307 | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 399 | rec = newLocalLogRec(ctx, tx, op.Value.Key, watchable.NewVersion(), true, timestamp) |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 400 | |
Adam Sadovsky | ebc183a | 2015-10-08 22:26:47 -0700 | [diff] [blame] | 401 | case watchable.OpSyncgroup: |
| 402 | vlog.Errorf("sync: convertLogRecord: watch LogEntry for syncgroup should not be converted: %v", logEnt) |
| 403 | return nil, verror.New(verror.ErrInternal, ctx, "cannot convert a watch log OpSyncgroup entry") |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 404 | |
| 405 | default: |
Raja Daoud | 4171c9c | 2015-07-14 20:07:44 -0700 | [diff] [blame] | 406 | vlog.Errorf("sync: convertLogRecord: invalid watch LogEntry: %v", logEnt) |
| 407 | return nil, verror.New(verror.ErrInternal, ctx, "cannot convert unknown watch log entry") |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 408 | } |
Raja Daoud | d454307 | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 409 | |
Raja Daoud | 4171c9c | 2015-07-14 20:07:44 -0700 | [diff] [blame] | 410 | return rec, nil |
Raja Daoud | d454307 | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | // newLocalLogRec creates a local sync log record given its information: key, |
| 414 | // version, deletion flag, and timestamp. It retrieves the current DAG head |
| 415 | // for the key (if one exists) to use as its parent (previous) version. |
Mike Burrows | 45573ce | 2015-11-19 18:02:12 -0800 | [diff] [blame] | 416 | func newLocalLogRec(ctx *context.T, tx store.Transaction, key, version []byte, deleted bool, timestamp int64) *LocalLogRec { |
| 417 | rec := LocalLogRec{} |
Raja Daoud | d454307 | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 418 | oid := string(key) |
Raja Daoud | 0dfdd25 | 2015-07-10 20:02:22 -0700 | [diff] [blame] | 419 | |
Raja Daoud | d454307 | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 420 | rec.Metadata.ObjId = oid |
| 421 | rec.Metadata.CurVers = string(version) |
| 422 | rec.Metadata.Delete = deleted |
| 423 | if head, err := getHead(ctx, tx, oid); err == nil { |
| 424 | rec.Metadata.Parents = []string{head} |
| 425 | } else if deleted || (verror.ErrorID(err) != verror.ErrNoExist.ID) { |
Raja Daoud | 4171c9c | 2015-07-14 20:07:44 -0700 | [diff] [blame] | 426 | vlog.Fatalf("sync: newLocalLogRec: cannot getHead to convert log record for %s: %v", oid, err) |
Raja Daoud | d454307 | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 427 | } |
| 428 | rec.Metadata.UpdTime = unixNanoToTime(timestamp) |
| 429 | return &rec |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Adam Sadovsky | ebc183a | 2015-10-08 22:26:47 -0700 | [diff] [blame] | 432 | // processSyncgroupLogRecord checks if the log entry is a syncgroup update and, |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 433 | // if it is, updates the watch prefixes for the app database and returns a |
| 434 | // syncgroup operation. Otherwise it returns nil with no other changes. |
| 435 | func processSyncgroupLogRecord(appName, dbName string, logEnt *watchable.LogEntry) *watchable.SyncgroupOp { |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 436 | switch op := logEnt.Op.(type) { |
Adam Sadovsky | ebc183a | 2015-10-08 22:26:47 -0700 | [diff] [blame] | 437 | case watchable.OpSyncgroup: |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 438 | gid, remove := op.Value.SgId, op.Value.Remove |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 439 | for _, prefix := range op.Value.Prefixes { |
| 440 | if remove { |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 441 | rmWatchPrefixSyncgroup(appName, dbName, prefix, gid) |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 442 | } else { |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 443 | addWatchPrefixSyncgroup(appName, dbName, prefix, gid) |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 444 | } |
| 445 | } |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 446 | vlog.VI(3).Infof("sync: processSyncgroupLogRecord: %s, %s: gid %d, remove %t, prefixes: %q", |
| 447 | appName, dbName, gid, remove, op.Value.Prefixes) |
| 448 | return &op.Value |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 449 | |
| 450 | default: |
Raja Daoud | 30225f3 | 2015-11-18 12:59:12 -0800 | [diff] [blame] | 451 | return nil |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
| 455 | // syncable returns true if the given log entry falls within the scope of a |
Adam Sadovsky | ebc183a | 2015-10-08 22:26:47 -0700 | [diff] [blame] | 456 | // syncgroup prefix for the given app database, and thus should be synced. |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 457 | // It is used to pre-filter the batch of log entries before sync processing. |
| 458 | func syncable(appdb string, logEnt *watchable.LogEntry) bool { |
| 459 | var key string |
| 460 | switch op := logEnt.Op.(type) { |
Raja Daoud | 7cb7179 | 2015-07-08 12:00:33 -0700 | [diff] [blame] | 461 | case watchable.OpPut: |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 462 | key = string(op.Value.Key) |
Raja Daoud | 7cb7179 | 2015-07-08 12:00:33 -0700 | [diff] [blame] | 463 | case watchable.OpDelete: |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 464 | key = string(op.Value.Key) |
Raja Daoud | 7cb7179 | 2015-07-08 12:00:33 -0700 | [diff] [blame] | 465 | case watchable.OpSyncSnapshot: |
Raja Daoud | d454307 | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 466 | key = string(op.Value.Key) |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 467 | default: |
| 468 | return false |
| 469 | } |
| 470 | |
Raja Daoud | d454307 | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 471 | // The key starts with one of the store's reserved prefixes for managed |
Adam Sadovsky | 819d4f1 | 2015-10-23 09:43:50 -0700 | [diff] [blame] | 472 | // namespaces (e.g. "$row", "$perms"). Remove that prefix before comparing it |
| 473 | // with the syncgroup prefixes which are defined by the application. |
| 474 | key = util.StripFirstKeyPartOrDie(key) |
Raja Daoud | d454307 | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 475 | |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 476 | for prefix := range watchPrefixes[appdb] { |
| 477 | if strings.HasPrefix(key, prefix) { |
| 478 | return true |
| 479 | } |
| 480 | } |
| 481 | return false |
| 482 | } |
| 483 | |
| 484 | // resMarkKey returns the key used to access the watcher resume marker. |
| 485 | func resMarkKey() string { |
| 486 | return util.JoinKeyParts(util.SyncPrefix, "w", "rm") |
| 487 | } |
| 488 | |
| 489 | // setResMark stores the watcher resume marker for a database. |
Sergey Rogulenko | 40402b5 | 2015-08-10 15:09:48 -0700 | [diff] [blame] | 490 | func setResMark(ctx *context.T, tx store.Transaction, resMark watch.ResumeMarker) error { |
Adam Sadovsky | a31a7cd | 2015-07-08 10:44:07 -0700 | [diff] [blame] | 491 | return util.Put(ctx, tx, resMarkKey(), resMark) |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // getResMark retrieves the watcher resume marker for a database. |
Sergey Rogulenko | 40402b5 | 2015-08-10 15:09:48 -0700 | [diff] [blame] | 495 | func getResMark(ctx *context.T, st store.StoreReader) (watch.ResumeMarker, error) { |
| 496 | var resMark watch.ResumeMarker |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 497 | key := resMarkKey() |
Adam Sadovsky | a31a7cd | 2015-07-08 10:44:07 -0700 | [diff] [blame] | 498 | if err := util.Get(ctx, st, key, &resMark); err != nil { |
Sergey Rogulenko | 40402b5 | 2015-08-10 15:09:48 -0700 | [diff] [blame] | 499 | return nil, err |
Raja Daoud | cb50b5d | 2015-06-26 18:37:24 -0700 | [diff] [blame] | 500 | } |
| 501 | return resMark, nil |
| 502 | } |