-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstance.go
1482 lines (1314 loc) · 44.2 KB
/
instance.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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 postgresql
import (
"database/sql"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/polarismesh/polaris/common/model"
"github.com/polarismesh/polaris/store"
apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
"go.uber.org/zap"
)
// instanceStore 实现了InstanceStore接口
type instanceStore struct {
master *BaseDB // 大部分操作都用主数据库
slave *BaseDB // 缓存相关的读取,请求到slave
}
// AddInstance 添加实例
func (ins *instanceStore) AddInstance(instance *model.Instance) error {
err := RetryTransaction("addInstance", func() error {
return ins.addInstance(instance)
})
return store.Error(err)
}
// addInstance
func (ins *instanceStore) addInstance(instance *model.Instance) error {
tx, err := ins.master.Begin()
if err != nil {
log.Errorf("[Store][database] add instance tx begin err: %s", err.Error())
return err
}
defer func() { _ = tx.Rollback() }()
// 新增数据之前,必须先清理老数据
if err := cleanInstance(tx, instance.ID()); err != nil {
return err
}
if err := addMainInstance(tx, instance); err != nil {
log.Errorf("[Store][database] add instance main insert err: %s", err.Error())
return err
}
if err := addInstanceCheck(tx, instance); err != nil {
return err
}
if err := updateInstanceMeta(tx, instance); err != nil {
log.Errorf("[Store][database] add instance meta err: %s", err.Error())
return err
}
if err := tx.Commit(); err != nil {
log.Errorf("[Store][database] add instance commit tx err: %s", err.Error())
return err
}
return nil
}
// BatchAddInstances 批量增加实例
func (ins *instanceStore) BatchAddInstances(instances []*model.Instance) error {
err := RetryTransaction("batchAddInstances", func() error {
return ins.batchAddInstances(instances)
})
return store.Error(err)
}
// batchAddInstances batch add instances
func (ins *instanceStore) batchAddInstances(instances []*model.Instance) error {
tx, err := ins.master.Begin()
if err != nil {
log.Errorf("[Store][database] batch add instances begin tx err: %s", err.Error())
return err
}
defer func() { _ = tx.Rollback() }()
if err := batchAddMainInstances(tx, instances); err != nil {
log.Errorf("[Store][database] batch add main instances err: %s", err.Error())
return err
}
if err := batchAddInstanceCheck(tx, instances); err != nil {
log.Errorf("[Store][database] batch add instance check err: %s", err.Error())
return err
}
if err := batchDeleteInstanceMeta(tx, instances); err != nil {
log.Errorf("[Store][database] batch delete instance metadata err: %s", err.Error())
return err
}
if err := batchAddInstanceMeta(tx, instances); err != nil {
log.Errorf("[Store][database] batch add instance metadata err: %s", err.Error())
return err
}
if err := tx.Commit(); err != nil {
log.Errorf("[Store][database] batch add instance commit tx err: %s", err.Error())
return err
}
return nil
}
// UpdateInstance 更新实例
func (ins *instanceStore) UpdateInstance(instance *model.Instance) error {
err := RetryTransaction("updateInstance", func() error {
return ins.updateInstance(instance)
})
if err == nil {
return nil
}
serr := store.Error(err)
if store.Code(serr) == store.DuplicateEntryErr {
serr = store.NewStatusError(store.DataConflictErr, err.Error())
}
return serr
}
// updateInstance update instance
func (ins *instanceStore) updateInstance(instance *model.Instance) error {
tx, err := ins.master.Begin()
if err != nil {
log.Errorf("[Store][database] update instance tx begin err: %s", err.Error())
return err
}
defer func() { _ = tx.Rollback() }()
// 更新main表
if err := updateInstanceMain(tx, instance); err != nil {
log.Errorf("[Store][database] update instance main err: %s", err.Error())
return err
}
// 更新health check表
if err := updateInstanceCheck(tx, instance); err != nil {
log.Errorf("[Store][database] update instance check err: %s", err.Error())
return err
}
// 更新meta表
if err := updateInstanceMeta(tx, instance); err != nil {
log.Errorf("[Store][database] update instance meta err: %s", err.Error())
return err
}
if err := tx.Commit(); err != nil {
log.Errorf("[Store][database] update instance commit tx err: %s", err.Error())
return err
}
return nil
}
// CleanInstance 清理数据
func (ins *instanceStore) CleanInstance(instanceID string) error {
return RetryTransaction("cleanInstance", func() error {
return ins.master.processWithTransaction("cleanInstance", func(tx *BaseTx) error {
if err := cleanInstance(tx, instanceID); err != nil {
return err
}
if err := tx.Commit(); err != nil {
log.Errorf("[Store][database] clean instance commit tx err: %s", err.Error())
return err
}
return nil
})
})
}
// cleanInstance 清理数据
// TODO 后续修改instance表,id外键删除级联,那么可以执行一次delete操作
func cleanInstance(tx *BaseTx, instanceID string) error {
log.Infof("[Store][database] clean instance(%s)", instanceID)
stmt, err := tx.Prepare("delete from instance where id = $1 and flag = 1")
if err != nil {
return store.Error(err)
}
if _, err := stmt.Exec(instanceID); err != nil {
log.Errorf("[Store][database] clean instance(%s), err: %s", instanceID, err.Error())
return store.Error(err)
}
return nil
}
// DeleteInstance 删除一个实例,删除实例实际上是把flag置为1
func (ins *instanceStore) DeleteInstance(instanceID string) error {
if instanceID == "" {
return errors.New("delete Instance Missing instance id")
}
return RetryTransaction("deleteInstance", func() error {
return ins.master.processWithTransaction("deleteInstance", func(tx *BaseTx) error {
stmt, err := tx.Prepare("update instance set flag = 1, mtime = CURRENT_TIMESTAMP where id = $1")
if err != nil {
return store.Error(err)
}
if _, err = stmt.Exec(instanceID); err != nil {
return store.Error(err)
}
if err := tx.Commit(); err != nil {
log.Errorf("[Store][database] delete instance commit tx err: %s", err.Error())
return err
}
return nil
})
})
}
// BatchDeleteInstances
func (ins *instanceStore) BatchDeleteInstances(ids []interface{}) error {
return RetryTransaction("batchDeleteInstance", func() error {
return ins.master.processWithTransaction("batchDeleteInstance", func(tx *BaseTx) error {
if err := BatchOperation("delete-instance", ids, func(objects []interface{}) error {
if len(objects) == 0 {
return nil
}
str := "update instance set flag = 1, mtime = CURRENT_TIMESTAMP"
placeholder, _ := PlaceholdersNI(len(objects), 1)
str += " where id in ( " + placeholder + ")"
stmt, err := tx.Prepare(str)
if err != nil {
return err
}
_, err = stmt.Exec(objects...)
return store.Error(err)
}); err != nil {
return err
}
if err := tx.Commit(); err != nil {
log.Errorf("[Store][database] batch delete instance commit tx err: %s", err.Error())
return err
}
return nil
})
})
}
// GetInstance 获取单个实例详情,只返回有效的数据
func (ins *instanceStore) GetInstance(instanceID string) (*model.Instance, error) {
instance, err := ins.getInstance(instanceID)
if err != nil {
return nil, err
}
// 如果实例无效,则不返回
if instance != nil && !instance.Valid {
return nil, nil
}
return instance, nil
}
// BatchGetInstanceIsolate 检查实例是否存在
func (ins *instanceStore) BatchGetInstanceIsolate(ids map[string]bool) (map[string]bool, error) {
if len(ids) == 0 {
return nil, nil
}
placeholder, _ := PlaceholdersNI(len(ids), 1)
str := "select id, isolate from instance where flag = 0 and id in (" + placeholder + ")"
args := make([]interface{}, 0, len(ids))
for key := range ids {
args = append(args, key)
}
instanceIsolate := make(map[string]bool, len(ids))
rows, err := ins.master.Query(str, args...)
if err != nil {
log.Errorf("[Store][database] check instances existed query err: %s", err.Error())
return nil, err
}
var idx string
var isolate int
for rows.Next() {
if err := rows.Scan(&idx, &isolate); err != nil {
log.Errorf("[Store][database] check instances existed scan err: %s", err.Error())
return nil, err
}
instanceIsolate[idx] = model.Int2bool(isolate)
}
return instanceIsolate, nil
}
// GetInstancesBrief 批量获取实例的serviceID
func (ins *instanceStore) GetInstancesBrief(ids map[string]bool) (map[string]*model.Instance, error) {
if len(ids) == 0 {
return nil, nil
}
placeholder, _ := PlaceholdersNI(len(ids), 1)
str := "select instance.id, host, port, name, namespace, token, platform_id " +
"from service, instance where instance.flag = 0 and service.flag = 0 " +
"and service.id = instance.service_id and instance.id in (" + placeholder + ")"
args := make([]interface{}, 0, len(ids))
for key := range ids {
args = append(args, key)
}
rows, err := ins.master.Query(str, args...)
if err != nil {
log.Errorf("[Store][database] get instances service token query err: %s", err.Error())
return nil, err
}
defer rows.Close()
out := make(map[string]*model.Instance, len(ids))
var item model.ExpandInstanceStore
var instance model.InstanceStore
item.ServiceInstance = &instance
for rows.Next() {
if err := rows.Scan(&instance.ID, &instance.Host, &instance.Port,
&item.ServiceName, &item.Namespace, &item.ServiceToken, &item.ServicePlatformID); err != nil {
log.Errorf("[Store][database] get instances service token scan err: %s", err.Error())
return nil, err
}
out[instance.ID] = model.ExpandStore2Instance(&item)
}
return out, nil
}
// GetInstancesCount 获取有效的实例总数
func (ins *instanceStore) GetInstancesCount() (uint32, error) {
countStr := "select count(*) from instance where flag = 0"
var count uint32
var err error
Retry("query-instance-row", func() error {
err = ins.master.QueryRow(countStr).Scan(&count)
return err
})
switch {
case err == sql.ErrNoRows:
return 0, nil
case err != nil:
log.Errorf("[Store][database] get instances count scan err: %s", err.Error())
return 0, err
default:
}
return count, nil
}
// GetInstancesMainByService 根据服务和host获取实例
// @note 不包括metadata
func (ins *instanceStore) GetInstancesMainByService(serviceID, host string) ([]*model.Instance, error) {
// 只查询有效的服务实例
str := genInstanceSelectSQL() + " where service_id = $1 and host = $2 and flag = 0"
rows, err := ins.master.Query(str, serviceID, host)
if err != nil {
log.Errorf("[Store][database] get instances main query err: %s", err.Error())
return nil, err
}
var out []*model.Instance
err = callFetchInstanceRows(rows, func(entry *model.InstanceStore) (b bool, e error) {
out = append(out, model.Store2Instance(entry))
return true, nil
})
if err != nil {
log.Errorf("[Store][database] call fetch instance rows err: %s", err.Error())
return nil, err
}
return out, nil
}
// GetExpandInstances 根据过滤条件查看对应服务实例及数目
func (ins *instanceStore) GetExpandInstances(filter, metaFilter map[string]string, offset uint32,
limit uint32) (uint32, []*model.Instance, error) {
// 只查询有效的实例列表
filter["instance.flag"] = "0"
out, err := ins.getExpandInstances(filter, metaFilter, offset, limit)
if err != nil {
return 0, nil, err
}
num, err := ins.getExpandInstancesCount(filter, metaFilter)
if err != nil {
return 0, nil, err
}
return num, out, err
}
// getExpandInstances 根据过滤条件查看对应服务实例
func (ins *instanceStore) getExpandInstances(filter, metaFilter map[string]string, offset uint32,
limit uint32) ([]*model.Instance, error) {
// 这种情况意味着,不需要详细的数据,可以不用query了
if limit == 0 {
return make([]*model.Instance, 0), nil
}
_, isName := filter["name"]
_, isNamespace := filter["namespace"]
_, isHost := filter["host"]
needForceIndex := isName || isNamespace || isHost
str := genExpandInstanceSelectSQL(needForceIndex)
order := &Order{"instance.mtime", "desc"}
str, args := genWhereSQLAndArgs(str, filter, metaFilter, order, offset, limit)
rows, err := ins.master.Query(str, args...)
if err != nil {
log.Errorf("[Store][database] get instance by filters query err: %s, str: %s, args: %v", err.Error(), str, args)
return nil, err
}
out, err := ins.getRowExpandInstances(rows)
if err != nil {
log.Errorf("[Store][database] get row instances err: %s", err.Error())
return nil, err
}
return out, nil
}
// getExpandInstancesCount 根据过滤条件查看对应服务实例的数目
func (ins *instanceStore) getExpandInstancesCount(filter, metaFilter map[string]string) (uint32, error) {
str := "select count(*) from instance "
// 查询条件是否有service表中的字段
_, isName := filter["name"]
_, isNamespace := filter["namespace"]
if isName || isNamespace {
str += "inner join service on instance.service_id = service.id "
}
str, args := genWhereSQLAndArgs(str, filter, metaFilter, nil, 0, 1)
var count uint32
var err error
Retry("query-instance-row", func() error {
err = ins.master.QueryRow(str, args...).Scan(&count)
return err
})
switch {
case err == sql.ErrNoRows:
log.Errorf("[Store][database] no row with this expand instance filter")
return count, err
case err != nil:
log.Errorf("[Store][database] get expand instance count by filter err: %s", err.Error())
return count, err
default:
return count, nil
}
}
// GetMoreInstances 根据mtime获取增量修改数据
// 这里会返回所有的数据的,包括valid=false的数据
// 对于首次拉取,firstUpdate=true,只会拉取flag!=1的数据
func (ins *instanceStore) GetMoreInstances(tx store.Tx, mtime time.Time, firstUpdate, needMeta bool,
serviceID []string) (map[string]*model.Instance, error) {
dbTx, _ := tx.GetDelegateTx().(*BaseTx)
if needMeta {
instances, err := ins.getMoreInstancesMainWithMeta(dbTx, mtime, firstUpdate, serviceID)
if err != nil {
return nil, err
}
return instances, nil
}
instances, err := ins.getMoreInstancesMain(dbTx, mtime, firstUpdate, serviceID)
if err != nil {
return nil, err
}
return instances, nil
}
// GetInstanceMeta 根据实例ID获取实例的metadata
func (ins *instanceStore) GetInstanceMeta(instanceID string) (map[string]string, error) {
str := "select mkey, mvalue from instance_metadata where id = $1"
rows, err := ins.master.Query(str, instanceID)
if err != nil {
log.Errorf("[Store][database] query instance meta err: %s", err.Error())
return nil, err
}
defer rows.Close()
out := make(map[string]string)
var key, value string
for rows.Next() {
if err := rows.Scan(&key, &value); err != nil {
log.Errorf("[Store][database] get instance meta rows scan err: %s", err.Error())
return nil, err
}
out[key] = value
}
if err := rows.Err(); err != nil {
log.Errorf("[Store][database] get instance meta rows next err: %s", err.Error())
return nil, err
}
return out, nil
}
// SetInstanceHealthStatus 设置实例健康状态
func (ins *instanceStore) SetInstanceHealthStatus(instanceID string, flag int, revision string) error {
return RetryTransaction("setInstanceHealthStatus", func() error {
return ins.master.processWithTransaction("setInstanceHealthStatus", func(tx *BaseTx) error {
stmt, err := tx.Prepare("update instance set health_status = $1, revision = $2, mtime = CURRENT_TIMESTAMP where id = $3")
if err != nil {
return store.Error(err)
}
if _, err := stmt.Exec(flag, revision, instanceID); err != nil {
return store.Error(err)
}
if err := tx.Commit(); err != nil {
log.Errorf("[Store][database] set instance health status commit tx err: %s", err.Error())
return err
}
return nil
})
})
}
// BatchSetInstanceHealthStatus 批量设置健康状态
func (ins *instanceStore) BatchSetInstanceHealthStatus(ids []interface{}, isolate int, revision string) error {
return RetryTransaction("batchSetInstanceHealthStatus", func() error {
return ins.master.processWithTransaction("batchSetInstanceHealthStatus", func(tx *BaseTx) error {
if err := BatchOperation("set-instance-healthy", ids, func(objects []interface{}) error {
if len(objects) == 0 {
return nil
}
var index = 1
str := fmt.Sprintf("update instance set health_status = $%d, revision = $%d, mtime = CURRENT_TIMESTAMP where id in ",
index, index+1)
index += 2
placeholder, _ := PlaceholdersNI(len(objects), index)
str += "(" + placeholder + ")"
args := make([]interface{}, 0, len(objects)+2)
args = append(args, isolate)
args = append(args, revision)
args = append(args, objects...)
stmt, err := tx.Prepare(str)
if err != nil {
return store.Error(err)
}
_, err = stmt.Exec(args...)
return store.Error(err)
}); err != nil {
return err
}
if err := tx.Commit(); err != nil {
log.Errorf("[Store][database] batch set instance health status commit tx err: %s", err.Error())
return err
}
return nil
})
})
}
// BatchSetInstanceIsolate 批量设置实例隔离状态
func (ins *instanceStore) BatchSetInstanceIsolate(ids []interface{}, isolate int, revision string) error {
return RetryTransaction("batchSetInstanceIsolate", func() error {
return ins.master.processWithTransaction("batchSetInstanceIsolate", func(tx *BaseTx) error {
if err := BatchOperation("set-instance-isolate", ids, func(objects []interface{}) error {
if len(objects) == 0 {
return nil
}
var index = 1
str := fmt.Sprintf("update instance set isolate = $%d, revision = $%d, mtime = CURRENT_TIMESTAMP where id in ",
index, index+1)
placeholder, _ := PlaceholdersNI(len(objects), index+2)
str += "(" + placeholder + ")"
args := make([]interface{}, 0, len(objects)+2)
args = append(args, isolate)
args = append(args, revision)
args = append(args, objects...)
stmt, err := tx.Prepare(str)
if err != nil {
return store.Error(err)
}
_, err = stmt.Exec(args...)
return store.Error(err)
}); err != nil {
return err
}
if err := tx.Commit(); err != nil {
log.Errorf("[Store][database] batch set instance isolate commit tx err: %s", err.Error())
return err
}
return nil
})
})
}
// BatchAppendInstanceMetadata 追加实例 metadata
func (ins *instanceStore) BatchAppendInstanceMetadata(requests []*store.InstanceMetadataRequest) error {
if len(requests) == 0 {
return nil
}
return ins.master.processWithTransaction("AppendInstanceMetadata", func(tx *BaseTx) error {
for i := range requests {
id := requests[i].InstanceID
revision := requests[i].Revision
metadata := requests[i].Metadata
str := "insert into instance_metadata(id, mkey, mvalue, ctime, mtime) values"
values := make([]string, 0, len(metadata))
args := make([]interface{}, 0, len(metadata)*3)
var index = 1
for k, v := range metadata {
values = append(values, fmt.Sprintf("($%d, $%d, $%d, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)",
index, index+1, index+2))
index += 3
args = append(args, id, k, v)
}
str += strings.Join(values, ",")
if log.DebugEnabled() {
log.Debug("[Store][database] append instance metadata", zap.String("sql", str), zap.Any("args", args))
}
stmt, err := tx.Prepare(str)
if err != nil {
return err
}
if _, err = stmt.Exec(args...); err != nil {
log.Errorf("[Store][database] append instance metadata err: %s", err.Error())
return err
}
stmt, err = tx.Prepare("update instance set revision = $1, mtime = CURRENT_TIMESTAMP where id = $2")
if err != nil {
return err
}
if _, err := stmt.Exec(revision, id); err != nil {
log.Errorf("[Store][database] append instance metadata update revision err: %s", err.Error())
return err
}
}
return tx.Commit()
})
}
// BatchRemoveInstanceMetadata 删除实例指定的 metadata
func (ins *instanceStore) BatchRemoveInstanceMetadata(requests []*store.InstanceMetadataRequest) error {
if len(requests) == 0 {
return nil
}
return ins.master.processWithTransaction("RemoveInstanceMetadata", func(tx *BaseTx) error {
for i := range requests {
id := requests[i].InstanceID
revision := requests[i].Revision
keys := requests[i].Keys
str := "delete from instance_metadata where id = $%d and mkey in (%s)"
values := make([]string, 0, len(keys))
args := make([]interface{}, 0, 1+len(keys))
args = append(args, id)
var index1 = 2
for i := range keys {
key := keys[i]
values = append(values, fmt.Sprintf("$%d", index1))
index1++
args = append(args, key)
}
str = fmt.Sprintf(str, 1, strings.Join(values, ","))
stmt, err := tx.Prepare(str)
if err != nil {
return err
}
if _, err = stmt.Exec(args...); err != nil {
log.Errorf("[Store][database] remove instance metadata by keys err: %s", err.Error())
return err
}
stmt, err = tx.Prepare("update instance set revision = $1, mtime = CURRENT_TIMESTAMP where id = $2")
if err != nil {
return err
}
if _, err := stmt.Exec(revision, id); err != nil {
log.Errorf("[Store][database] remove instance metadata by keys update revision err: %s", err.Error())
return err
}
}
return tx.Commit()
})
}
// getInstance 内部获取instance函数,根据instanceID,直接读取元数据,不做其他过滤
func (ins *instanceStore) getInstance(instanceID string) (*model.Instance, error) {
str := genInstanceSelectSQL() + " where instance.id = $1"
rows, err := ins.master.Query(str, instanceID)
if err != nil {
log.Errorf("[Store][database] get instance query err: %s", err.Error())
return nil, err
}
out, err := fetchInstanceRows(rows)
if err != nil {
return nil, err
}
if len(out) == 0 {
return nil, err
}
meta, err := ins.GetInstanceMeta(out[0].ID())
if err != nil {
return nil, err
}
out[0].MallocProto()
out[0].Proto.Metadata = meta
return out[0], nil
}
// getMoreInstancesMainWithMeta 获取增量instance+healthcheck+meta内容
// @note ro库有多个实例,且主库到ro库各实例的同步时间不一致。为避免获取不到meta,需要采用一条sql语句获取全部数据
func (ins *instanceStore) getMoreInstancesMainWithMeta(tx *BaseTx, mtime time.Time, firstUpdate bool, serviceID []string) (
map[string]*model.Instance, error) {
// 首次拉取
if firstUpdate {
// 获取全量服务实例
instances, err := ins.getMoreInstancesMain(tx, mtime, firstUpdate, serviceID)
if err != nil {
log.Errorf("[Store][database] get more instance main err: %s", err.Error())
return nil, err
}
// 获取全量服务实例元数据
str := "select id, mkey, mvalue from instance_metadata"
rows, err := ins.slave.Query(str)
if err != nil {
log.Errorf("[Store][database] acquire instances meta query err: %s", err.Error())
return nil, err
}
if err := fetchInstanceMetaRows(instances, rows); err != nil {
return nil, err
}
return instances, nil
}
// 非首次拉取
var index = 1
str := genCompleteInstanceSelectSQL() + fmt.Sprintf(" where instance.mtime >= $%d", index)
args := make([]interface{}, 0, len(serviceID)+1)
args = append(args, mtime)
if len(serviceID) > 0 {
placeholder, _ := PlaceholdersNI(len(serviceID), index+1)
str += " and service_id in (" + placeholder
str += ")"
}
for _, id := range serviceID {
args = append(args, id)
}
rows, err := ins.slave.Query(str, args...)
if err != nil {
log.Errorf("[Store][database] get more instance query err: %s", err.Error())
return nil, err
}
return fetchInstanceWithMetaRows(rows)
}
// fetchInstanceWithMetaRows 获取instance main+health_check+instance_metadata rows里面的数据
func fetchInstanceWithMetaRows(rows *sql.Rows) (map[string]*model.Instance, error) {
if rows == nil {
return nil, nil
}
defer rows.Close()
out := make(map[string]*model.Instance)
var (
item model.InstanceStore
id, mKey, mValue string
//ctime, mtime time.Time
ctimeStr, mtimeStr string
)
progress := 0
for rows.Next() {
progress++
if progress%100000 == 0 {
log.Infof("[Store][database] instance+meta fetch rows progress: %d", progress)
}
if err := rows.Scan(&item.ID, &item.ServiceID, &item.VpcID, &item.Host, &item.Port, &item.Protocol,
&item.Version, &item.HealthStatus, &item.Isolate, &item.Weight, &item.EnableHealthCheck,
&item.LogicSet, &item.Region, &item.Zone, &item.Campus, &item.Priority, &item.Revision,
&item.Flag, &item.CheckType, &item.TTL, &id, &mKey, &mValue, &ctimeStr, &mtimeStr); err != nil {
log.Errorf("[Store][database] fetch instance+meta rows err: %s", err.Error())
return nil, err
}
// 将字符串转换为int64
ctimeFloat, err := strconv.ParseFloat(ctimeStr, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse create_time: %v", err)
}
mtimeFloat, err := strconv.ParseFloat(mtimeStr, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse modify_time: %v", err)
}
item.CreateTime = int64(ctimeFloat)
item.ModifyTime = int64(mtimeFloat)
if _, ok := out[item.ID]; !ok {
out[item.ID] = model.Store2Instance(&item)
}
// 实例存在meta
if id != "" {
if out[item.ID].Proto.Metadata == nil {
out[item.ID].Proto.Metadata = make(map[string]string)
}
out[item.ID].Proto.Metadata[mKey] = mValue
}
}
if err := rows.Err(); err != nil {
log.Errorf("[Store][database] fetch instance+metadata rows next err: %s", err.Error())
return nil, err
}
return out, nil
}
// getMoreInstancesMain 获取增量instances 主表内容,health_check内容
func (ins *instanceStore) getMoreInstancesMain(tx *BaseTx, mtime time.Time, firstUpdate bool, serviceID []string) (
map[string]*model.Instance, error) {
var index = 1
str := genInstanceSelectSQL() + fmt.Sprintf(" where instance.mtime >= $%d", index)
args := make([]interface{}, 0, len(serviceID)+1)
args = append(args, mtime)
if firstUpdate {
str += " and flag != 1"
}
if len(serviceID) > 0 {
placeholder, _ := PlaceholdersNI(len(serviceID), index+1)
str += " and service_id in (" + placeholder
str += ")"
}
for _, id := range serviceID {
args = append(args, id)
}
rows, err := tx.Query(str, args...)
if err != nil {
log.Errorf("[Store][database] get more instance query err: %s", err.Error())
return nil, err
}
out := make(map[string]*model.Instance)
err = callFetchInstanceRows(rows, func(entry *model.InstanceStore) (b bool, e error) {
out[entry.ID] = model.Store2Instance(entry)
return true, nil
})
if err != nil {
log.Errorf("[Store][database] call fetch instance rows err: %s", err.Error())
return nil, err
}
return out, nil
}
// getRowExpandInstances 根据rows获取对应expandInstance
func (ins *instanceStore) getRowExpandInstances(rows *sql.Rows) ([]*model.Instance, error) {
out, err := fetchExpandInstanceRows(rows)
if err != nil {
return nil, err
}
data := make([]interface{}, 0, len(out))
for idx := range out {
data = append(data, out[idx].Proto)
}
err = BatchQuery("expand-instance-metadata", data, func(objects []interface{}) error {
return ins.batchAcquireInstanceMetadata(objects)
})
if err != nil {
log.Errorf("[Store][database] get expand instances metadata err: %s", err.Error())
return nil, err
}
return out, nil
}
// batchAcquireInstanceMetadata 批量获取instance的metadata信息
// web端获取实例的数据的时候使用
func (ins *instanceStore) batchAcquireInstanceMetadata(instances []interface{}) error {
rows, err := batchQueryMetadata(ins.master.Query, instances)
if err != nil {
return err
}
if rows == nil {
return nil
}
defer rows.Close()
out := make(map[string]map[string]string)
var id, key, value string
for rows.Next() {
if err := rows.Scan(&id, &key, &value); err != nil {
log.Errorf("[Store][database] multi query instance metadata rows scan err: %s", err.Error())
return err
}
if _, ok := out[id]; !ok {
out[id] = make(map[string]string)
}
out[id][key] = value
}
if err := rows.Err(); err != nil {
log.Errorf("[Store][database] multi query instance metadata rows next err: %s", err.Error())
return err
}
// 把接收到的metadata,设置到instance中
// 这里会有两层循环,后续可以优化 TODO
for id, meta := range out {
for _, ele := range instances {
if id == ele.(*apiservice.Instance).GetId().GetValue() {
ele.(*apiservice.Instance).Metadata = meta
break
}
}
}
return nil
}
// batchQueryMetadata 批量查找metadata
func batchQueryMetadata(queryHandler QueryHandler, instances []interface{}) (*sql.Rows, error) {
if len(instances) == 0 {
return nil, nil
}
str := "select id, mkey, mvalue from instance_metadata where id in("
first := true
args := make([]interface{}, 0, len(instances))
index := 1
for _, ele := range instances {
if first {
str += fmt.Sprintf("$%d", index)
first = false
} else {
str += fmt.Sprintf(",$%d", index)
}
index++
args = append(args, ele.(*apiservice.Instance).GetId().GetValue())
}
str += ")"
rows, err := queryHandler(str, args...)
if err != nil {
log.Errorf("[Store][database] multi query instance metadata err: %s", err.Error())
return nil, err
}
return rows, nil
}
// addMainInstance 往instance主表中增加数据
func addMainInstance(tx *BaseTx, instance *model.Instance) error {
str := "insert into instance(id, service_id, vpc_id, host, port, protocol, " +
"version, health_status, isolate, weight, enable_health_check, logic_set, " +
"cmdb_region, cmdb_zone, cmdb_idc, priority, revision, ctime, mtime) " +
"values($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, " +
"$15, $16, $17, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)"
stmt, err := tx.Prepare(str)
if err != nil {
return err
}
healthy := 0
if instance.Healthy() {