/
manipulator.go
837 lines (651 loc) · 21.5 KB
/
manipulator.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
// Copyright 2019 Aporeto Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package manipvortex
import (
"context"
"fmt"
"sync"
"time"
"go.aporeto.io/elemental"
"go.aporeto.io/manipulate"
"go.uber.org/zap"
)
// updater is the type of all crud functions.
type updater func(mctx manipulate.Context, object elemental.Identifiable) error
// vortexManipulator is a Vortex based on the memdb implementation.
type vortexManipulator struct {
upstreamManipulator manipulate.Manipulator
upstreamSubscriber manipulate.Subscriber
downstreamManipulator manipulate.Manipulator
model elemental.ModelManager
processors map[string]*Processor
commitIdentityEvent map[string]struct{}
subscribers []*vortexSubscriber
transactionQueue chan *Transaction
enableLog bool
logfile string
logChannel chan *Transaction
defaultReadConsistency manipulate.ReadConsistency
defaultWriteConsistency manipulate.WriteConsistency
defaultQueueDuration time.Duration
pageSize int
prefetcher Prefetcher
upstreamReconciler Reconciler
downstreamReconciler Reconciler
disableUpstreamCommit bool
sync.RWMutex
}
// New will create a new cache. Caller must provide a valid
// backend manipulator and susbscriber. If the manipulator is nil, it will be assumed
// that the cache is standalone (ie there is no backend to synchronize with).
func New(
ctx context.Context,
downstreamManipulator manipulate.Manipulator,
processors map[string]*Processor,
model elemental.ModelManager,
options ...Option,
) (manipulate.BufferedManipulator, error) {
if downstreamManipulator == nil {
panic("downstreamManipulator must not be nil")
}
if model == nil {
panic("model must not be nil")
}
if len(processors) == 0 {
panic("processors must not be empty")
}
cfg := newConfig()
for _, option := range options {
option(cfg)
}
m := &vortexManipulator{
downstreamManipulator: downstreamManipulator,
upstreamManipulator: cfg.upstreamManipulator,
upstreamSubscriber: cfg.upstreamSubscriber,
defaultReadConsistency: cfg.readConsistency,
defaultWriteConsistency: cfg.writeConsistency,
disableUpstreamCommit: cfg.disableUpstreamCommit,
enableLog: cfg.enableLog,
logfile: cfg.logfile,
pageSize: cfg.defaultPageSize,
prefetcher: cfg.prefetcher,
upstreamReconciler: cfg.upstreamReconciler,
downstreamReconciler: cfg.downstreamReconciler,
processors: processors,
model: model,
transactionQueue: cfg.transactionQueue,
defaultQueueDuration: cfg.defaultQueueDuration,
subscribers: []*vortexSubscriber{},
commitIdentityEvent: map[string]struct{}{},
}
if m.enableLog {
c, err := newLogWriter(ctx, m.logfile, 100)
if err != nil {
return nil, fmt.Errorf("unable open commit log file: %s", err)
}
m.logChannel = c
}
if m.prefetcher != nil {
if err := m.warmUp(ctx); err != nil {
return nil, fmt.Errorf("unable to warm up: %s", err)
}
}
if m.upstreamSubscriber != nil {
filter := elemental.NewPushFilter()
for identity, cfg := range m.processors {
if cfg.CommitOnEvent {
m.commitIdentityEvent[identity] = struct{}{}
}
filter.FilterIdentity(identity)
}
m.upstreamSubscriber.Start(ctx, filter)
go m.monitor(ctx)
}
// Start the background thread. It will be blocked
// when we do resyncs and this is ok. We want it blocked
// so that resync continues while any updates are buffered.
go m.backgroundSync(ctx)
return m, nil
}
// Flush implements the flush interface of the Vortex. It will flush
// all the cache for write-through. For write-back it will wait
// for a maximum of 10 seconds for transactions to complete. When
// done it will flush the channel and create a completely fresh
// db.
func (m *vortexManipulator) Flush(ctx context.Context) error {
m.RLock()
defer m.RUnlock()
if m.prefetcher != nil {
m.prefetcher.Flush()
}
f, ok := m.downstreamManipulator.(manipulate.FlushableManipulator)
if ok {
// Wait for the channel to clean up
maxDelay := time.Now().Add(10 * time.Second)
for len(m.transactionQueue) > 0 && time.Now().Before(maxDelay) {
time.Sleep(1 * time.Second)
}
// Flush any outstanding transactions and restart the backgrond sync
if err := f.Flush(ctx); err != nil {
return fmt.Errorf("unable to flush the datastore: %s", err)
}
}
if m.prefetcher != nil {
if err := m.warmUp(ctx); err != nil {
return err
}
}
return nil
}
func (m *vortexManipulator) RetrieveMany(mctx manipulate.Context, dest elemental.Identifiables) error {
m.RLock()
defer m.RUnlock()
if mctx == nil {
mctx = manipulate.NewContext(context.Background())
}
if m.prefetcher != nil {
prefetched, err := m.prefetcher.Prefetch(mctx.Context(), elemental.OperationRetrieveMany, dest.Identity(), m.upstreamManipulator, mctx.Derive())
if err != nil {
return fmt.Errorf("unable to prefetch data for retrieve many operation for '%s': %s", dest.Identity(), err)
}
if err := m.insertPrefetchedData(prefetched); err != nil {
return fmt.Errorf("unable to insert prefetched data for retrieve many operation for '%s': %s", dest.Identity(), err)
}
}
if !m.shouldProcess(mctx, dest.Identity()) {
if m.upstreamManipulator != nil {
return m.upstreamManipulator.RetrieveMany(mctx, dest)
}
return nil
}
if cfg := m.processors[dest.Identity().Name]; cfg != nil && cfg.RetrieveManyHook != nil {
commit, err := cfg.RetrieveManyHook(m.downstreamManipulator, mctx, dest)
if !commit {
return err
}
}
return m.downstreamManipulator.RetrieveMany(mctx, dest)
}
func (m *vortexManipulator) Retrieve(mctx manipulate.Context, object elemental.Identifiable) error {
m.RLock()
defer m.RUnlock()
if mctx == nil {
mctx = manipulate.NewContext(context.Background())
}
if m.prefetcher != nil {
prefetched, err := m.prefetcher.Prefetch(mctx.Context(), elemental.OperationRetrieve, object.Identity(), m.upstreamManipulator, mctx.Derive())
if err != nil {
return fmt.Errorf("unable to prefetch data for retrieve operation for '%s': %s", object.Identity(), err)
}
if err := m.insertPrefetchedData(prefetched); err != nil {
return fmt.Errorf("unable to insert prefetched data for retrieve operation for '%s': %s", object.Identity(), err)
}
}
// If we are not processing the object, we send it upstream.
// We only deal with CRUDs.
if !m.shouldProcess(mctx, object.Identity()) {
if m.upstreamManipulator != nil {
return m.upstreamManipulator.Retrieve(mctx, object)
}
return nil
}
if err := m.downstreamManipulator.Retrieve(mctx, object); err != nil {
// If we can't find it locally, and its strong consistency retrieve
// we will try the backend if we have one.
if m.upstreamManipulator == nil || !isStrongReadConsistency(mctx, m.processors[object.Identity().Name], m.defaultReadConsistency) {
return err
}
if err := m.upstreamManipulator.Retrieve(mctx, object); err != nil {
return err
}
// Make sure that we update our cache for future reference.
if err := m.downstreamManipulator.Create(mctx, object); err != nil {
return fmt.Errorf("unable to update local cache from backend: %s", err)
}
}
return nil
}
func (m *vortexManipulator) Create(mctx manipulate.Context, object elemental.Identifiable) error {
m.RLock()
defer m.RUnlock()
return m.coreCRUDOperation(elemental.OperationCreate, mctx, object)
}
func (m *vortexManipulator) Update(mctx manipulate.Context, object elemental.Identifiable) error {
m.RLock()
defer m.RUnlock()
return m.coreCRUDOperation(elemental.OperationUpdate, mctx, object)
}
func (m *vortexManipulator) Delete(mctx manipulate.Context, object elemental.Identifiable) error {
m.RLock()
defer m.RUnlock()
return m.coreCRUDOperation(elemental.OperationDelete, mctx, object)
}
func (m *vortexManipulator) DeleteMany(mctx manipulate.Context, identity elemental.Identity) error {
m.RLock()
defer m.RUnlock()
if mctx == nil {
mctx = manipulate.NewContext(context.Background())
}
if m.upstreamManipulator == nil {
return fmt.Errorf("delete many not supported by vortexManipulator")
}
return m.upstreamManipulator.DeleteMany(mctx, identity)
}
func (m *vortexManipulator) Count(mctx manipulate.Context, identity elemental.Identity) (int, error) {
m.RLock()
defer m.RUnlock()
if mctx == nil {
mctx = manipulate.NewContext(context.Background())
}
if m.downstreamManipulator == nil {
return 0, fmt.Errorf("datastore is not initialized")
}
return m.downstreamManipulator.Count(mctx, identity)
}
func (m *vortexManipulator) hasBackendSubscriber() bool {
m.RLock()
defer m.RUnlock()
return m.upstreamSubscriber != nil
}
func (m *vortexManipulator) registerSubscriber(s manipulate.Subscriber) {
m.Lock()
defer m.Unlock()
m.subscribers = append(m.subscribers, s.(*vortexSubscriber))
}
// UpdateFilter updates the current filter.
func (m *vortexManipulator) updateFilter() {
m.Lock()
defer m.Unlock()
if m.upstreamSubscriber == nil {
return
}
m.commitIdentityEvent = map[string]struct{}{}
filter := elemental.NewPushFilter()
for identity := range m.processors {
m.commitIdentityEvent[identity] = struct{}{}
filter.FilterIdentity(identity)
}
for _, subscriber := range m.subscribers {
subscriber.RLock()
for callerIdentity := range subscriber.filter.Identities {
cfg, ok := m.processors[callerIdentity]
if ok {
// If we are processing this event and there is a client
// subscription, we will only commit if the corresponding
// flag is set. Otherwise, the client will have to handle
// the update, so we remove it from the list.
if !cfg.CommitOnEvent {
delete(m.commitIdentityEvent, callerIdentity)
}
continue
}
// If it is not one of the registered identites and the client
// has subscribed anyway, we still register and forward it to the
// client.
filter.FilterIdentity(callerIdentity)
}
subscriber.RUnlock()
}
// Update the downstream filter.
m.upstreamSubscriber.UpdateFilter(filter)
}
// coreCRUDOperation implements the basic operation for updates of the db. This is used
// by create, update, and delete.
func (m *vortexManipulator) coreCRUDOperation(operation elemental.Operation, mctx manipulate.Context, object elemental.Identifiable) error {
if mctx == nil {
mctx = manipulate.NewContext(context.Background())
}
// If the identity is not registered or the request has a parent
// send upstream. We are not dealing with this locally.
if !m.shouldProcess(mctx, object.Identity()) {
return m.commitUpstream(mctx.Context(), operation, mctx, object)
}
reconcile, err := m.genericUpdater(operation, mctx, object)
if err != nil {
return err
}
if !reconcile {
return nil
}
return m.commitLocal(operation, mctx, object)
}
// shouldProcess returns true if the request can be processed by the cache. If false,
// it must be forwarded to the upstream.
func (m *vortexManipulator) shouldProcess(mctx manipulate.Context, identity elemental.Identity) bool {
_, ok := m.processors[identity.Name]
if !ok {
return false
}
return mctx == nil || mctx.Parent() == nil
}
// commitUpstream will commit a transaction to the upstream if it is not nil. It will
// return the upstream error.
func (m *vortexManipulator) commitUpstream(ctx context.Context, operation elemental.Operation, mctx manipulate.Context, object elemental.Identifiable) error {
if m.upstreamManipulator == nil || m.disableUpstreamCommit {
return nil
}
var reconcile bool
var err error
// If we have an accepter, we see if it accepts the write
if m.upstreamReconciler != nil {
object, reconcile, err = m.upstreamReconciler.Reconcile(mctx, operation, object)
if err != nil {
return err
}
if !reconcile {
return nil
}
}
// If it is managed object we apply the pre-hook.
cfg, ok := m.processors[object.Identity().Name]
if ok && cfg.UpstreamReconciler != nil {
object, reconcile, err = cfg.UpstreamReconciler.Reconcile(mctx, operation, object)
if err != nil {
return err
}
if !reconcile {
return nil
}
}
// We always commit if prehook says ok or it is not a managed object.
if err := m.methodFromType(operation)(mctx, object); err != nil {
return err
}
return nil
}
// commitLocal will commit a transaction locally after processing any
// hooks. It will return error if either the hook or the local commit
// fail for some reason.
func (m *vortexManipulator) commitLocal(operation elemental.Operation, mctx manipulate.Context, object elemental.Identifiable) error {
var reconcile bool
var err error
if mctx == nil {
mctx = manipulate.NewContext(context.Background())
}
// If we have a global Reconciler, we see if it accepts the write.
if m.downstreamReconciler != nil {
object, reconcile, err = m.downstreamReconciler.Reconcile(mctx, operation, object)
if err != nil {
return err
}
if !reconcile {
return nil
}
}
cfg, ok := m.processors[object.Identity().Name]
if !ok {
return nil
}
// If we have a processor Reconciler, we see if it accepts the write.
if cfg.DownstreamReconciler != nil {
object, reconcile, err = cfg.DownstreamReconciler.Reconcile(mctx, operation, object)
if err != nil {
return err
}
if !reconcile {
return nil
}
}
if err := m.localMethodFromType(operation)(mctx, object); err != nil {
return err
}
if m.enableLog {
m.logChannel <- &Transaction{
Date: time.Now(),
mctx: mctx,
Object: object,
Method: operation,
}
}
return nil
}
// localMethodFromType will return a pointer to the corresponding function
// based on the elemental method type.
func (m *vortexManipulator) localMethodFromType(method elemental.Operation) updater {
switch method {
case elemental.OperationCreate:
return m.downstreamManipulator.Create
case elemental.OperationUpdate:
return m.downstreamManipulator.Update
default:
return m.downstreamManipulator.Delete
}
}
// methodFromType it will return an upstream function pointer based on the method.
func (m *vortexManipulator) methodFromType(method elemental.Operation) updater {
switch method {
case elemental.OperationCreate:
return m.upstreamManipulator.Create
case elemental.OperationUpdate:
return m.upstreamManipulator.Update
default:
return m.upstreamManipulator.Delete
}
}
// genericUpdate will implement the updates. It takes as parameters the methods
// to be used (update, create, delete) and avoids repeating code. It will
// return true if the transaction has to be committed in the local DB. It will
// return an error if the backend fails. Specifically:
// For WriteThrough: it will return an error if the backend fails.
// For WriteBack it will cache it and return commit=false. The commit will happen
// later after the object is stored in the backend.
func (m *vortexManipulator) genericUpdater(method elemental.Operation, mctx manipulate.Context, object elemental.Identifiable) (bool, error) {
if m.upstreamManipulator == nil || m.disableUpstreamCommit {
return true, nil
}
// We are guaranteed that there is at least one object and the identity is processable.
processor := m.processors[object.Identity().Name]
// In Strong consistency we make sure that the backend gets the create.
// Only then store in the cache.
if isStrongWriteConsistency(mctx, processor, m.defaultWriteConsistency) {
return true, m.commitUpstream(mctx.Context(), method, mctx, object)
}
tdeadline := processor.QueueingDuration
if tdeadline == 0 {
tdeadline = m.defaultQueueDuration
}
select {
case m.transactionQueue <- &Transaction{
mctx: mctx,
Object: object,
Method: method,
Deadline: time.Now().Add(tdeadline),
}:
return false, nil
default:
return false, fmt.Errorf("commit queue is full: %d", len(m.transactionQueue))
}
}
// backgroundSync will empty the transaction queue and try to sync it
// with the backend.
func (m *vortexManipulator) backgroundSync(ctx context.Context) {
if m.upstreamManipulator == nil {
return
}
for {
select {
case t := <-m.transactionQueue:
// If the dealine is exceeded we just drop the request
// no matter what. This allows us to clean up the queue
// if there is a problem.
if time.Now().After(t.Deadline) {
continue
}
if t.Object == nil {
continue
}
// We first try to update the backend. If this succeeds
// then we also update the local db. At this point
// the object can be accessible through our API since
// the ID has been populated.
m.RLock()
if _, ok := m.processors[t.Object.Identity().Name]; !ok {
m.RUnlock()
continue
}
retryCtx, cancel := context.WithDeadline(ctx, t.Deadline)
defer cancel()
if err := m.commitUpstream(retryCtx, t.Method, t.mctx, t.Object); err != nil {
m.RUnlock()
zap.L().Error("failed to commit object upstream", zap.Error(err))
continue
}
// Update the local copy of the object now.
if err := m.commitLocal(t.Method, t.mctx, t.Object); err != nil {
zap.L().Error("failed to commit object downstream", zap.Error(err))
}
m.RUnlock()
case <-ctx.Done():
// TODO: If we get killed with objects in the queue, then what ?
// Do we ignore it and try to empty all objects or what ????
return
}
}
}
// monitor registers for events for all the identities of interest
// and keeps the local cache up-to-date with the backend.
func (m *vortexManipulator) monitor(ctx context.Context) {
for {
select {
case evt := <-m.upstreamSubscriber.Events():
m.RLock()
_, commit := m.commitIdentityEvent[evt.Identity]
m.RUnlock()
if commit {
if err := m.eventHandler(ctx, evt); err != nil {
m.pushErrors(fmt.Errorf("unable to handle event: %s", err))
continue
}
}
m.pushEvent(evt)
case err := <-m.upstreamSubscriber.Errors():
m.pushErrors(fmt.Errorf("upstream error: %s", err))
case status := <-m.upstreamSubscriber.Status():
switch status {
case manipulate.SubscriberStatusReconnection:
// We resync everything
if err := m.Flush(ctx); err != nil {
m.pushErrors(fmt.Errorf("unable to flush: %s", err))
}
case manipulate.SubscriberStatusFinalDisconnection:
m.pushStatus(status)
return
}
m.pushStatus(status)
case <-ctx.Done():
return
}
}
}
func (m *vortexManipulator) pushEvent(evt *elemental.Event) {
m.RLock()
defer m.RUnlock()
var isFiltered bool
for _, s := range m.subscribers {
s.RLock()
isFiltered = false
if s.filter != nil {
isFiltered = s.filter.IsFilteredOut(evt.Identity, evt.Type)
}
s.RUnlock()
if !isFiltered {
select {
case s.subscriberEventChannel <- evt.Duplicate():
default:
zap.L().Error("Subscriber event channel is full")
}
}
}
}
func (m *vortexManipulator) pushStatus(status manipulate.SubscriberStatus) {
m.RLock()
defer m.RUnlock()
for _, s := range m.subscribers {
select {
case s.subscriberStatusChannel <- status:
default:
zap.L().Error("Subscriber status channel is full", zap.Int("status", int(status)))
}
}
}
func (m *vortexManipulator) pushErrors(err error) {
m.RLock()
defer m.RUnlock()
for _, s := range m.subscribers {
select {
case s.subscriberErrorChannel <- err:
default:
zap.L().Error("Subscriber error channel is full", zap.Error(err))
}
}
}
func (m *vortexManipulator) eventHandler(ctx context.Context, evt *elemental.Event) error {
if m.upstreamManipulator == nil {
return nil
}
obj := m.model.IdentifiableFromString(evt.Identity)
if err := evt.Decode(obj); err != nil {
return fmt.Errorf("unable to unmarshal received event: %s", err)
}
m.RLock()
defer m.RUnlock()
// DO WE FORCE COMPLETE RESYNCS HERE IF THERE ARE FAILURES?
// ERROR HANDLING NEEDS WORK. Since errors here are extremely
// unlikely, provided that the schema is correct, probably the
// right thing to do is to force a re-sync or completely panic.
var method elemental.Operation
switch evt.Type {
case elemental.EventCreate:
method = elemental.OperationCreate
case elemental.EventUpdate:
method = elemental.OperationUpdate
case elemental.EventDelete:
method = elemental.OperationDelete
default:
return fmt.Errorf("unsupported event received: %s", evt.Type)
}
if err := m.commitLocal(method, nil, obj); err != nil {
if method != elemental.OperationDelete {
return fmt.Errorf("unable to commit event of type '%s': %s", evt.Type, err)
}
}
return nil
}
func (m *vortexManipulator) insertPrefetchedData(prefetched elemental.Identifiables) error {
if prefetched == nil {
return nil
}
lst := prefetched.List()
if len(lst) == 0 {
return nil
}
for _, item := range lst {
if err := m.commitLocal(elemental.OperationCreate, nil, item); err != nil {
return err
}
}
return nil
}
func (m *vortexManipulator) warmUp(ctx context.Context) error {
if m.upstreamManipulator == nil {
return nil
}
for _, proc := range m.processors {
prefetched, err := m.prefetcher.WarmUp(ctx, m.upstreamManipulator, m.model, proc.Identity)
if err != nil {
return fmt.Errorf("unable to prefetch '%s for warm up operation: %s", proc.Identity, err)
}
if err := m.insertPrefetchedData(prefetched); err != nil {
return fmt.Errorf("unable to insert prefetched '%s' for warm up operation: %s", proc.Identity, err)
}
}
return nil
}