-
Notifications
You must be signed in to change notification settings - Fork 210
/
PluginAndCfgsSnapshot.java
944 lines (818 loc) · 41.5 KB
/
PluginAndCfgsSnapshot.java
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
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
* <p>
* http:https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 com.qlangtech.tis.plugin;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.qlangtech.tis.TIS;
import com.qlangtech.tis.async.message.client.consumer.impl.MQListenerFactory;
import com.qlangtech.tis.config.flink.IFlinkCluster;
import com.qlangtech.tis.coredefine.module.action.TargetResName;
import com.qlangtech.tis.datax.IDataxProcessor;
import com.qlangtech.tis.datax.impl.DataxProcessor;
import com.qlangtech.tis.extension.ExtensionList;
import com.qlangtech.tis.extension.PluginManager;
import com.qlangtech.tis.extension.PluginWrapper;
import com.qlangtech.tis.extension.impl.PluginManifest;
import com.qlangtech.tis.manage.common.CenterResource;
import com.qlangtech.tis.manage.common.Config;
import com.qlangtech.tis.manage.common.ConfigFileContext;
import com.qlangtech.tis.manage.common.HttpUtils;
import com.qlangtech.tis.manage.common.TisUTF8;
import com.qlangtech.tis.maven.plugins.tpi.PluginClassifier;
import com.qlangtech.tis.plugin.incr.TISSinkFactory;
import com.qlangtech.tis.trigger.util.JsonUtil;
import com.qlangtech.tis.util.HeteroEnum;
import com.qlangtech.tis.util.PluginMeta;
import com.qlangtech.tis.util.RobustReflectionConverter2;
import com.qlangtech.tis.util.UploadPluginMeta;
import com.qlangtech.tis.web.start.TisSubModule;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.jar.Attributes;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* @author: 百岁([email protected])
* @create: 2022-04-02 09:15
**/
public class PluginAndCfgsSnapshot {
public static final String TIS_APP_NAME = "tis_app_name";
private static final Logger logger = LoggerFactory.getLogger(PluginAndCfgsSnapshot.class);
private static PluginAndCfgsSnapshot pluginAndCfgsSnapshot;
public static String getTaskEntryName() {
// if (taskId < 1) {
// throw new IllegalArgumentException("taskId shall be set");
// }
// return "task_xxxx" + taskId;
return "task_xxxx";
}
public static PluginAndCfgsSnapshot getRepositoryCfgsSnapshot(String resName, InputStream manifestJar) throws IOException {
return getRepositoryCfgsSnapshot(resName, manifestJar, true);
}
/**
* 远程传输过来的资源快照信息
*
* @param resName
* @param manifestJar
* @return
* @throws IOException
*/
public static PluginAndCfgsSnapshot getRepositoryCfgsSnapshot(String resName, InputStream manifestJar,
boolean resetConfigWithSysProps) throws IOException {
PluginAndCfgsSnapshot pluginAndCfgsSnapshot = null;
String appName = null;
// for (URL lib : libraryURLs) {
//try (
JarInputStream jarReader = new JarInputStream(manifestJar);
//) {
Manifest manifest = jarReader.getManifest();
appName = getRepositoryCfgsSnapshot(resName, manifest);
// KeyedPluginStore.PluginMetas.KEY_GLOBAL_PLUGIN_STORE;
//Attributes pluginMetas = manifest.getAttributes(Config.KEY_PLUGIN_METAS);
// processPluginMetas(pluginMetas);
pluginAndCfgsSnapshot =
PluginAndCfgsSnapshot.setLocalPluginAndCfgsSnapshot(PluginAndCfgsSnapshot.deserializePluginAndCfgsSnapshot(new TargetResName(appName), manifest));
Attributes sysProps = manifest.getAttributes(Config.KEY_JAVA_RUNTIME_PROP_ENV_PROPS);
if (resetConfigWithSysProps) {
Config.setConfig(null);
System.setProperty(Config.KEY_JAVA_RUNTIME_PROP_ENV_PROPS, String.valueOf(true));
StringBuffer sysPropsDesc = new StringBuffer();
for (Map.Entry<Object, Object> pluginDesc : sysProps.entrySet()) {
Attributes.Name name = (Attributes.Name) pluginDesc.getKey();
String val = (String) pluginDesc.getValue();
String key = PluginAndCfgsSnapshot.convertCfgPropertyKey(name.toString(), false);
System.setProperty(key, val);
sysPropsDesc.append("\n").append(key).append("->").append(val);
}
logger.info("sysProps details:" + sysPropsDesc.toString());
}
// @see TISFlinkCDCStreamFactory 在这个类中进行配置信息的加载
// shall not have any exception here.
TisSubModule.TIS_CONSOLE.getLaunchPort();
Config.getInstance();
//}
if (pluginAndCfgsSnapshot == null) {
throw new IllegalStateException("param appName can not be null,in res name:" + resName);
}
// }
return pluginAndCfgsSnapshot;
}
// private static void processPluginMetas(Attributes pluginMetas) {
// pluginMetas.getValue(KeyedPluginStore.PluginMetas.KEY_GLOBAL_PLUGIN_STORE);
// pluginMetas.getValue(KeyedPluginStore.PluginMetas.KEY_PLUGIN_META);
// pluginMetas.getValue(KeyedPluginStore.PluginMetas.KEY_APP_LAST_MODIFY_TIMESTAMP);
// }
public static String getRepositoryCfgsSnapshot(String resName, Manifest manifest) {
Attributes tisAppName = manifest.getAttributes(PluginAndCfgsSnapshot.TIS_APP_NAME);
String appName = null;
// Attributes pluginInventory = manifest.getAttributes("plugin_inventory");
if (tisAppName == null) {
throw new IllegalStateException("tisAppName can not be empty in lib:" + resName);
}
aa:
for (Map.Entry<Object, Object> pluginDesc : tisAppName.entrySet()) {
Attributes.Name name = (Attributes.Name) pluginDesc.getKey();
String val = (String) pluginDesc.getValue();
appName = name.toString();
break aa;
// pluginManager.dynamicLoadPlugin(String.valueOf(pluginDesc.getKey()));
}
return appName;
}
public static String convertCfgPropertyKey(String key, boolean serialize) {
return serialize ? org.apache.commons.lang3.StringUtils.replace(key, ".", "_") :
org.apache.commons.lang3.StringUtils.replace(key, "_", ".");
}
public static PluginAndCfgsSnapshot setLocalPluginAndCfgsSnapshot(PluginAndCfgsSnapshot snapshot) {
return pluginAndCfgsSnapshot = snapshot;
}
private final TargetResName collection;
/**
* key:fileName val:lastModifyTimestamp
*/
public final Map<String, Long> globalPluginStoreLastModify;
public final Set<PluginMeta> pluginMetas;
private final Set<IRepositoryResource> repoRes;
/**
* 应用相关配置目录的最后更新时间
*/
public final Long appLastModifyTimestamp;
private final Optional<KeyedPluginStore.PluginMetas> appMetas;
public PluginAndCfgsSnapshot(TargetResName collection, Map<String, Long> globalPluginStoreLastModify,
IPluginMetasInfo metasInfo, Long appLastModifyTimestamp,
KeyedPluginStore.PluginMetas appMetas) {
this.globalPluginStoreLastModify = globalPluginStoreLastModify;
this.pluginMetas = metasInfo.getMetas();
this.repoRes = metasInfo.getRepoResources();
this.appLastModifyTimestamp = appLastModifyTimestamp;
this.collection = collection;
this.appMetas = Optional.ofNullable(appMetas);
}
public static void createManifestCfgAttrs2File(File manifestJar, StoreResourceType resourceType, TargetResName collection, long timestamp,
Optional<Predicate<PluginMeta>> pluginMetasFilter) throws Exception {
createManifestCfgAttrs2File(manifestJar, resourceType, collection, timestamp, pluginMetasFilter, Collections.emptyMap());
}
public static Manifest createDataBatchJobManifestCfgAttrs(TargetResName collection) throws Exception {
IDataxProcessor processor = DataxProcessor.load(null, StoreResourceType.DataApp, collection.getName());
return createDataBatchJobManifestCfgAttrs(processor);
}
/**
* 通过运行时遍历的方式取得 DataX 批量任务对应的Manifest
*
* @param processor
* @return
* @throws Exception
*/
public static Manifest createDataBatchJobManifestCfgAttrs(IDataxProcessor processor) throws Exception {
if (processor.getResType() != StoreResourceType.DataApp) {
throw new IllegalArgumentException("resType must be " + StoreResourceType.DataApp + " but now is " + processor.getResType());
}
RobustReflectionConverter2.PluginMetas pluginMetas =
RobustReflectionConverter2.PluginMetas.collectMetas((metas) -> {
// 先收集plugmeta,特别是通过dataXWriter的dataSource关联的元数据
IDataxProcessor dataxProcessor = DataxProcessor.load(null, processor.getResType(), processor.identityValue());
dataxProcessor.getReaders(null).forEach((reader) -> {
// reader.getSelectedTabs().forEach((tab) -> tab.getCols());
reader.startScanDependency();
});
dataxProcessor.getWriter(null).startScanDependency();
});
TargetResName resName = new TargetResName(processor.identityValue());
// return createManifestCfgAttrs(resName, System.currentTimeMillis(), Collections.emptyMap(), () -> {
// PluginAndCfgsSnapshot localSnapshot = getLocalPluginAndCfgsSnapshot(processor.getResType(), resName, Optional.empty(), pluginMetas);
// return localSnapshot;
// }).getRight();
return createManifestCfgAttrs(resName, System.currentTimeMillis()
, Collections.emptyMap(), () -> {
KeyedPluginStore.PluginMetas metas
= KeyedPluginStore.getAppAwarePluginMetas(processor.getResType(), resName.getName(), false);
ComponentMeta dataxComponentMeta = new ComponentMeta(Lists.newArrayList(pluginMetas.getRepoResources()));
Map<String, Long> globalPluginStoreLastModify = ComponentMeta.getGlobalPluginStoreLastModifyTimestamp(dataxComponentMeta);
return new PluginAndCfgsSnapshot(resName, globalPluginStoreLastModify
, pluginMetas //
, metas.lastModifyTimestamp, metas);
}).getValue();
}
/**
* 通过运行时遍历的方式取得到Manifest
*
* @param collection
* @param timestamp
* @return
* @throws Exception
*/
public static Manifest createFlinkIncrJobManifestCfgAttrs(TargetResName collection, long timestamp) throws Exception {
// Manifest manifest = null;
StoreResourceType resourceType = StoreResourceType.DataApp;
RobustReflectionConverter2.PluginMetas pluginMetas =
RobustReflectionConverter2.PluginMetas.collectMetas((metas) -> {
MQListenerFactory sourceFactory = HeteroEnum.getIncrSourceListenerFactory(collection.getName());
sourceFactory.create();
// 先收集plugmeta,特别是通过dataXWriter的dataSource关联的元数据
IDataxProcessor processor = DataxProcessor.load(null, resourceType, collection.getName());
TISSinkFactory incrSinKFactory = TISSinkFactory.getIncrSinKFactory(collection.getName());
incrSinKFactory.createSinkFunction(processor);
});
return createFlinkIncrJobManifestCfgAttrs(resourceType, collection, timestamp, pluginMetas);
}
public static Pair<PluginAndCfgsSnapshot, Manifest> createManifestCfgAttrs2File(File manifestJar, StoreResourceType resourceType,
TargetResName collection,
long timestamp,
Optional<Predicate<PluginMeta>> pluginMetasFilter, Map<String, String> extraEnvProps) throws Exception {
Pair<PluginAndCfgsSnapshot, Manifest> manifestCfgAttrs = createManifestCfgAttrs(resourceType, collection, timestamp,
extraEnvProps, pluginMetasFilter, new IPluginMetasInfo() {
});
PluginAndCfgsSnapshotUtils.writeManifest2Jar(manifestJar, manifestCfgAttrs.getRight());
return manifestCfgAttrs;
}
public static Manifest createFlinkIncrJobManifestCfgAttrs(StoreResourceType resourceType, TargetResName collection, long timestamp,
IPluginMetasInfo appendPluginMeta) throws Exception {
return createManifestCfgAttrs(resourceType, collection, timestamp, Optional.empty(), new IPluginMetasInfo() {
@Override
public Set<PluginMeta> getMetas() {
return Sets.union(appendPluginMeta.getMetas(),
Collections.singleton(new PluginMeta(TISSinkFactory.KEY_PLUGIN_TPI_CHILD_PATH + collection.getName(),
Config.getMetaProps().getVersion(), Optional.empty())));
}
@Override
public Set<IRepositoryResource> getRepoResources() {
return appendPluginMeta.getRepoResources();
}
});
}
public static Manifest createManifestCfgAttrs(StoreResourceType resourceType, TargetResName collection, long timestamp,
Optional<Predicate<PluginMeta>> pluginMetasFilter,
IPluginMetasInfo appendPluginMeta) throws Exception {
return createManifestCfgAttrs(resourceType, collection, timestamp, Collections.emptyMap(), pluginMetasFilter, appendPluginMeta).getRight();
}
public static Pair<PluginAndCfgsSnapshot, Manifest> //
createManifestCfgAttrs(StoreResourceType resourceType, TargetResName collection,
long timestamp,
Map<String, String> extraEnvProps,
Optional<Predicate<PluginMeta>> pluginMetasFilter, IPluginMetasInfo appendPluginMeta) throws Exception {
return createManifestCfgAttrs(collection, timestamp, extraEnvProps, () -> {
PluginAndCfgsSnapshot localSnapshot = getLocalPluginAndCfgsSnapshot(resourceType, collection, pluginMetasFilter, appendPluginMeta);
return localSnapshot;
});
// //=====================================================================
// if (!CenterResource.notFetchFromCenterRepository()) {
// throw new IllegalStateException("must not fetchFromCenterRepository");
// }
//
// Manifest manifest = new Manifest();
// Map<String, Attributes> entries = manifest.getEntries();
// Attributes attrs = new Attributes();
// attrs.put(new Attributes.Name(collection.getName()), String.valueOf(timestamp));
// // 传递App名称
// entries.put(TIS_APP_NAME, attrs);
//
// final Attributes cfgAttrs = new Attributes();
// // 传递Config变量
// Config.getInstance().visitKeyValPair((e) -> {
// if (Config.KEY_TIS_HOST.equals(e.getKey())) {
// // tishost为127.0.0.1会出错
// return;
// }
// addCfgAttrs(cfgAttrs, e);
// });
// for (Map.Entry<String, String> e : extraEnvProps.entrySet()) {
// addCfgAttrs(cfgAttrs, e);
// }
// cfgAttrs.put(new Attributes.Name(convertCfgPropertyKey(Config.KEY_TIS_HOST, true)), Config.getTisHost());
// entries.put(Config.KEY_JAVA_RUNTIME_PROP_ENV_PROPS, cfgAttrs);
//
//
// //"globalPluginStore" "pluginMetas" "appLastModifyTimestamp"
//
//
// PluginAndCfgsSnapshot localSnapshot = getLocalPluginAndCfgsSnapshot(resourceType, collection, pluginMetasFilter,
// appendPluginMeta);
//
// localSnapshot.attachPluginCfgSnapshot2Manifest(manifest);
// return ImmutablePair.of(localSnapshot, manifest);
}
public static Pair<PluginAndCfgsSnapshot, Manifest> //
createManifestCfgAttrs(TargetResName collection,
long timestamp,
Map<String, String> extraEnvProps,
Supplier<PluginAndCfgsSnapshot> localPluginAndCfgsSnapshotCreator) throws Exception {
//=====================================================================
if (!CenterResource.notFetchFromCenterRepository()) {
throw new IllegalStateException("must not fetchFromCenterRepository");
}
Manifest manifest = new Manifest();
Map<String, Attributes> entries = manifest.getEntries();
Attributes attrs = new Attributes();
attrs.put(new Attributes.Name(collection.getName()), String.valueOf(timestamp));
// 传递App名称
entries.put(TIS_APP_NAME, attrs);
final Attributes cfgAttrs = new Attributes();
// 传递Config变量
Config.getInstance().visitKeyValPair((e) -> {
if (Config.KEY_TIS_HOST.equals(e.getKey())) {
// tishost为127.0.0.1会出错
return;
}
addCfgAttrs(cfgAttrs, e);
});
for (Map.Entry<String, String> e : extraEnvProps.entrySet()) {
addCfgAttrs(cfgAttrs, e);
}
cfgAttrs.put(new Attributes.Name(convertCfgPropertyKey(Config.KEY_TIS_HOST, true)), Config.getTisHost());
entries.put(Config.KEY_JAVA_RUNTIME_PROP_ENV_PROPS, cfgAttrs);
PluginAndCfgsSnapshot localSnapshot = localPluginAndCfgsSnapshotCreator.get();
localSnapshot.attachPluginCfgSnapshot2Manifest(manifest);
return ImmutablePair.of(localSnapshot, manifest);
}
private static void addCfgAttrs(Attributes cfgAttrs, Map.Entry<String, String> e) {
cfgAttrs.put(new Attributes.Name(convertCfgPropertyKey(e.getKey(), true)), e.getValue());
}
private static void collectAllPluginMeta(PluginMeta meta, PluginMetaSet collector) {
// meta.getLastModifyTimeStamp();
collector.add(meta);
List<PluginMeta> dpts = meta.getMetaDependencies();
collector.addAll(dpts);
for (PluginMeta m : dpts) {
collectAllPluginMeta(m, collector);
}
}
public Set<String> getPluginNames() {
return pluginMetas.stream().map((m) -> m.getPluginName()).collect(Collectors.toSet());
}
/**
* Flink 远端 会启多个VM,一个JM,多个TM,问题是多个VM在运行时 本地插件/配置的目录对应的是一个,为了避免当其中一个VM更新之后,其他VM就中的pluginManager->UberClassLoader就
* 不更新了,所以本地需要有两个配置快照的副本,一个是localSnaphsot,另外一个 cacheSnaphsot
*
* @param localSnaphsot 每次从本地文件系统中去load
* @param cacheSnaphsot 缓存在VM内存中的,用来和最新的远端快照做对比
* @throws Exception
*/
public void synchronizTpisAndConfs(PluginAndCfgsSnapshot localSnaphsot,
Optional<PluginAndCfgsSnapshot> cacheSnaphsot) throws Exception {
synchronized (TIS.class) {
logger.info("start synchronizTpisAndConfs,cacheSnaphsot.isPresent():" + cacheSnaphsot.isPresent() + ",TIS.initialized:" + TIS.initialized);
this.synchronizTpisAndConfs(localSnaphsot);
if (cacheSnaphsot.isPresent() && TIS.initialized /** 必须要TIS 已经初始化 完成,启动时 cacheSnaphsot
内的依赖plugin为空致使启动报错*/) {
this.updatePluginManager(cacheSnaphsot.get());
}
}
}
/**
* 通过将远程仓库中的plugin tpi的最近更新时间和本地tpi的最新更新时间经过对比,计算出需要更新的插件集合
*
* @param localSnaphsot
* @return
*/
private void synchronizTpisAndConfs(PluginAndCfgsSnapshot localSnaphsot) throws Exception {
if (!localSnaphsot.appMetas.isPresent()) {
throw new IllegalArgumentException("localSnaphsot.appMetas must be present");
}
StringBuffer updateTpisLogger = new StringBuffer("\nplugin synchronizTpisAndConfs" +
"------------------------------\n");
Long localTimestamp;
File cfg = null;
//boolean cfgChanged = false;
// URL globalCfg = null;
updateTpisLogger.append(">>global cfg compare:\n");
for (Map.Entry<String, Long> entry : this.globalPluginStoreLastModify.entrySet()) {
localTimestamp = localSnaphsot.globalPluginStoreLastModify.get(entry.getKey());
if (localTimestamp == null || entry.getValue() > localTimestamp) {
// 更新本地配置文件
//globalCfg = CenterResource.getPathURL(Config.SUB_DIR_CFG_REPO, TIS.KEY_TIS_PLUGIN_CONFIG + "/" +
// entry.getKey());
cfg = CenterResource.copyFromRemote2Local(Config.KEY_TIS_PLUGIN_CONFIG + "/" + entry.getKey(), true);
FileUtils.writeStringToFile(PluginStore.getLastModifyTimeStampFile(cfg),
String.valueOf(entry.getValue()), TisUTF8.get());
// cfgChanged = true;
updateTpisLogger.append(entry.getKey()).append(localTimestamp == null ?
"[" + entry.getValue() + "] " + "local is none" :
" center ver:" + entry.getValue() + " > " + "local ver:" + localTimestamp).append("\n");
}
}
updateTpisLogger.append(">>app cfg compare:\n");
updateTpisLogger.append("center:").append(this.appLastModifyTimestamp)
.append(this.appLastModifyTimestamp > localSnaphsot.appLastModifyTimestamp ? " > " : " <= ")
.append("local:").append(localSnaphsot.appLastModifyTimestamp).append("\n");
if (this.appLastModifyTimestamp > localSnaphsot.appLastModifyTimestamp) {
// 更新app相关配置,下载并更新本地配置
KeyedPluginStore.AppKey appKey = new KeyedPluginStore.AppKey(null, StoreResourceType.parse(false),
this.collection.getName(), (KeyedPluginStore.PluginClassCategory) null);
URL appCfgUrl = CenterResource.getPathURL(Config.SUB_DIR_CFG_REPO,
Config.KEY_TIS_PLUGIN_CONFIG + "/" + appKey.getSubDirPath());
updateTpisLogger.append("app cfg url:" + appCfgUrl + " config file enum:");
KeyedPluginStore.PluginMetas appMetas = localSnaphsot.appMetas.get();
HttpUtils.get(appCfgUrl, new ConfigFileContext.StreamProcess<Void>() {
@Override
public Void p(int status, InputStream stream, Map<String, List<String>> headerFields) {
try {
FileUtils.deleteQuietly(appMetas.appDir);
ZipInputStream zipInput = new ZipInputStream(stream);
ZipEntry entry = null;
while ((entry = zipInput.getNextEntry()) != null) {
try (OutputStream output = FileUtils.openOutputStream(new File(appMetas.appDir,
entry.getName()))) {
updateTpisLogger.append(entry.getName()).append(",");
IOUtils.copy(zipInput, output);
}
zipInput.closeEntry();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
}
});
updateTpisLogger.append("\n");
// cfgChanged = true;
}
Set<PluginMeta> result = getShallUpdatePluginMeta(localSnaphsot, updateTpisLogger);
int copyCount = 0;
for (PluginMeta update : result) {
if (update.copyFromRemote(Collections.emptyList(), true, true)) {
copyCount++;
}
}
updateTpisLogger.append("success synchronized files count:" + copyCount).append("\n");
// TIS tis = TIS.get();
// PluginManager pluginManager = tis.getPluginManager();
// Set<PluginMeta> loaded = Sets.newHashSet();
// PluginWrapperList batch = new PluginWrapperList();
// for (PluginMeta update : result) {
// dynamicLoad(pluginManager, update, batch, result, loaded);
// }
//
// if (batch.size() > 0) {
// pluginManager.start(batch);
// updateTpisLogger.append("\ndynamic reload plugins:" + batch.getBatchNames());
// }
// Thread.sleep(3000l);
// if (cfgChanged) {
// TIS.cleanPluginStore();
// tis.cleanExtensionCache();
// }
logger.info(updateTpisLogger.append("\n------------------------------").toString());
// return result;
}
/**
* 更新本地pluginManger,激活插件
*
* @param localCacheSnaphsot
* @throws Exception
*/
private void updatePluginManager(PluginAndCfgsSnapshot localCacheSnaphsot) throws Exception {
StringBuffer updateTpisLogger = new StringBuffer("\nplugin updatePluginManager " + "synchronize" +
"------------------------------\n");
Set<PluginMeta> result = getShallUpdatePluginMeta(localCacheSnaphsot, updateTpisLogger);
// for (PluginMeta update : result) {
// update.copyFromRemote(Collections.emptyList(), true, true);
// }
try {
TIS tis = TIS.get();
PluginManager pluginManager = tis.getPluginManager();
Set<PluginMeta> loaded = Sets.newHashSet();
PluginWrapperList batch = new PluginWrapperList();
for (PluginMeta update : result) {
dynamicLoad(pluginManager, update, batch, result, loaded);
}
if (batch.size() > 0) {
pluginManager.start(batch);
updateTpisLogger.append("\ndynamic reload plugins:" + batch.getBatchNames());
Thread.sleep(3000l);
TIS.cleanPluginStore();
tis.cleanExtensionCache();
}
} catch (Exception e) {
logger.error(updateTpisLogger.append("\n------------------------------").toString());
throw e;
}
logger.info(updateTpisLogger.append("\n------------------------------").toString());
// return result;
}
private Set<PluginMeta> getShallUpdatePluginMeta(PluginAndCfgsSnapshot localSnaphsot,
StringBuffer updateTpisLogger) {
Set<PluginMeta> result = new HashSet<>();
updateTpisLogger.append(">>center repository:").append(pluginMetas.stream().map((meta) -> meta.toString()).collect(Collectors.joining(",")));
updateTpisLogger.append("\n>>local:").append(localSnaphsot.pluginMetas.stream().map((meta) -> meta.toString()).collect(Collectors.joining(","))).append("\n");
updateTpisLogger.append(">>compare result\n");
Map<String, PluginMeta> locals =
localSnaphsot.pluginMetas.stream().collect(Collectors.toMap((m) -> m.getKey(), (m) -> m));
PluginMeta m = null;
for (PluginMeta meta : pluginMetas) {
if (IFlinkCluster.SKIP_PLUGIN_NAMES.contains(meta.getPluginName())) {
continue;
}
m = locals.get(meta.getKey());
if (m == null || meta.getLastModifyTimeStamp() > m.getLastModifyTimeStamp()) {
result.add(meta);
updateTpisLogger.append(meta.getKey()).append(m == null ? " local is none" : " center repository " +
"ver:" + meta.getLastModifyTimeStamp() + " > local ver:" + m.getLastModifyTimeStamp()).append("\n");
}
}
return result;
}
/**
* 为了去除batch plugin中的重复机器,用一个List包裹一下
*/
public static class PluginWrapperList {
List<PluginWrapper> batch = Lists.newArrayList();
Set<String> addPluginNams = Sets.newHashSet();
public PluginWrapperList() {
}
public PluginWrapperList(PluginWrapper pluginWrapper) {
this.add(pluginWrapper);
}
public PluginWrapperList(List<PluginWrapper> plugins) {
plugins.forEach((p) -> {
add(p);
});
}
public void add(PluginWrapper plugin) {
if (addPluginNams.add(plugin.getShortName())) {
batch.add(plugin);
}
}
public List<PluginWrapper> getPlugins() {
return this.batch;
}
public Map<String, PluginWrapper> getPluginsByName() {
return batch.stream().collect(Collectors.toMap(PluginWrapper::getShortName, p -> p));
}
public Set<ClassLoader> getLoaders() {
return batch.stream().map(p -> p.classLoader).collect(Collectors.toSet());
}
public int size() {
return batch.size();
}
public String getBatchNames() {
return this.batch.stream().map(p -> p.getShortName()).collect(Collectors.joining(","));
}
public boolean contains(PluginWrapper depender) {
for (PluginWrapper wrapper : batch) {
if (StringUtils.equals(wrapper.getShortName(), depender.getShortName())) {
return true;
}
}
return false;
// return batch.contains( depender);
}
}
private void dynamicLoad(PluginManager pluginManager, PluginMeta update, PluginWrapperList batch,
Set<PluginMeta> shallUpdate, Set<PluginMeta> loaded) {
try {
for (PluginMeta dpt : update.getMetaDependencies()) {
this.dynamicLoad(pluginManager, dpt, batch, shallUpdate, loaded);
}
if (shallUpdate.contains(update) && loaded.add(update)) {
pluginManager.dynamicLoad(update.getPluginPackageFile(), true, batch);
}
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
public TargetResName getAppName() {
return this.collection;
}
/**
* snapshot 从manifest中反序列化出来
*
* @param app
* @param manifest
* @return
*/
public static PluginAndCfgsSnapshot deserializePluginAndCfgsSnapshot(TargetResName app, Manifest manifest) {
Map<String, Long> globalPluginStoreLastModify = Maps.newHashMap();
// Long appLastModifyTimestamp;
Attributes pluginMetas = manifest.getAttributes(Config.KEY_PLUGIN_METAS);
String[] globalPluginStoreSeri =
StringUtils.split(pluginMetas.getValue(KeyedPluginStore.PluginMetas.KEY_GLOBAL_PLUGIN_STORE), ",");
String[] file2timestamp = null;
for (String p : globalPluginStoreSeri) {
file2timestamp = StringUtils.split(p, PluginMeta.NAME_VER_SPLIT);
if (file2timestamp.length != 2) {
throw new IllegalStateException("file2timestamp length must be 2,val:" + p);
}
globalPluginStoreLastModify.put(file2timestamp[0], Long.parseLong(file2timestamp[1]));
}
JSONArray ms = null;
String metsAttr = null;
try {
metsAttr = pluginMetas.getValue(Config.KEY_PLUGIN_METAS);
ms = JSONArray.parseArray(metsAttr);
} catch (Exception e) {
throw new RuntimeException("illegal metaAttr:" + metsAttr, e);
}
List<PluginMeta> metas = PluginMeta.parse(JsonUtil.toArray(String.class, ms));
metas.forEach((meta) -> {
if (meta.isLastModifyTimeStampNull()) {
throw new IllegalStateException("pluginMeta:" + meta.getKey() + " relevant LastModify timestamp can " + "not be null");
}
});
return new PluginAndCfgsSnapshot(app, globalPluginStoreLastModify, new IPluginMetasInfo() {
@Override
public Set<PluginMeta> getMetas() {
return Sets.newHashSet(metas);
}
}, Long.parseLong(pluginMetas.getValue(KeyedPluginStore.PluginMetas.KEY_APP_LAST_MODIFY_TIMESTAMP)), null);
}
/**
* 远端执行点的本地快照
*
* @param collection
* @param appendPluginMeta
* @return
*/
public static PluginAndCfgsSnapshot getWorkerPluginAndCfgsSnapshot(StoreResourceType resourceType, TargetResName collection,
Set<PluginMeta> appendPluginMeta) {
return getLocalPluginAndCfgsSnapshot(resourceType, collection, (pluginMetas, dataxComponentMeta) -> {
PluginMetaSet collector = new PluginMetaSet(Optional.empty());
File pluginDir = getPluginRootDir();
Collection<File> tpis = FileUtils.listFiles(pluginDir,
new String[]{PluginClassifier.PACAKGE_TPI_EXTENSION_NAME}, false);
tpis.forEach((tpi) -> {
PluginManifest manifest = PluginManifest.create(tpi);
if (manifest != null) {
collector.add(manifest.getPluginMeta());
}
});
for (PluginMeta m : appendPluginMeta) {
collectAllPluginMeta(m, collector);
}
return collector;
});
}
public static File getPluginRootDir() {
return new File(Config.getLibDir(), TIS.KEY_TIS_PLUGIN_ROOT);
}
/**
* @param collection
* @param pluginMetasFilter pluginMetas 有plugin tpi 不需要同步(属于特例)
* @param appendPluginMeta
* @return
*/
private static PluginAndCfgsSnapshot //
getLocalPluginAndCfgsSnapshot(StoreResourceType resourceType, TargetResName collection,
Optional<Predicate<PluginMeta>> pluginMetasFilter, IPluginMetasInfo appendPluginMeta) {
// ExtensionList<HeteroEnum> hlist = TIS.get().getExtensionList(HeteroEnum.class);
return getLocalPluginAndCfgsSnapshot(resourceType, collection, (pluginMetas, dataxComponentMeta) -> {
appendPluginMeta.getRepoResources().forEach((res) -> {
dataxComponentMeta.addResource(res);
});
PluginMetaSet collector = new PluginMetaSet(pluginMetasFilter);
for (PluginMeta m : pluginMetas.metas) {
collectAllPluginMeta(m, collector);
}
Set<PluginMeta> globalPluginMetas = dataxComponentMeta.loadPluginMeta();
for (PluginMeta m : globalPluginMetas) {
collectAllPluginMeta(m, collector);
}
for (PluginMeta m : appendPluginMeta.getMetas()) {
collectAllPluginMeta(m, collector);
}
return collector;
});
}
private static PluginAndCfgsSnapshot getLocalPluginAndCfgsSnapshot(StoreResourceType resourceType, TargetResName collection,
MetaSetProductor metaSetProductor) {
// 本次任务相关插件元信息
KeyedPluginStore.PluginMetas pluginMetas = KeyedPluginStore.getAppAwarePluginMetas(resourceType, collection.getName());
// Set<PluginMeta> globalPluginMetas = null;
Map<String, Long> gPluginStoreLastModify = Collections.emptyMap();
UploadPluginMeta upm = UploadPluginMeta.parse("x:require", true);
TIS tis = TIS.get();
List<IRepositoryResource> keyedPluginStores = Collections.emptyList();
if (tis != null) {
ExtensionList<HeteroEnum> hlist = TIS.get().getExtensionList(HeteroEnum.class);
keyedPluginStores =
hlist.stream()
.filter((e) -> !e.isAppNameAware())
.flatMap((e) -> e.getPluginStore(null, upm).getAll().stream())
.collect(Collectors.toList());
}
ComponentMeta dataxComponentMeta = new ComponentMeta(keyedPluginStores);
PluginMetaSet pluginMetaSet = metaSetProductor.call(pluginMetas, dataxComponentMeta);
gPluginStoreLastModify = ComponentMeta.getGlobalPluginStoreLastModifyTimestamp(dataxComponentMeta);
try {
return new PluginAndCfgsSnapshot(collection, gPluginStoreLastModify //
, pluginMetaSet //
, pluginMetas.lastModifyTimestamp, pluginMetas);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
interface MetaSetProductor {
PluginMetaSet call(KeyedPluginStore.PluginMetas pluginMetas, ComponentMeta dataxComponentMeta);
}
private static class PluginMetaSet implements IPluginMetasInfo {
private HashSet<PluginMeta> metas = Sets.newHashSet();
private Set<IRepositoryResource> repoRes = Sets.newHashSet();
final Optional<Predicate<PluginMeta>> pluginMetasFilter;
public PluginMetaSet(Optional<Predicate<PluginMeta>> pluginMetasFilter) {
this.pluginMetasFilter = pluginMetasFilter;
}
@Override
public Set<PluginMeta> getMetas() {
return (pluginMetasFilter.isPresent() ?
this.metas.stream().filter(pluginMetasFilter.get()).collect(Collectors.toSet()) : this.metas);
}
@Override
public Set<IRepositoryResource> getRepoResources() {
return this.repoRes;
}
public boolean add(PluginMeta meta) {
Iterator<PluginMeta> it = metas.iterator();
PluginMeta m = null;
while (it.hasNext()) {
m = it.next();
if (StringUtils.equals(meta.getPluginName(), m.getPluginName())) {
if (meta.ver.compareTo(m.ver) > 0) {
it.remove();
return metas.add(meta);
// 新加的版本高,需要将之前的版本替换
// System.out.println("meta.ver:" + meta.ver + ",m.ver:" + m.ver);
}
return false;
}
}
return metas.add(meta);
}
public boolean addAll(Collection<PluginMeta> c) {
for (PluginMeta m : c) {
this.add(m);
}
return true;
}
}
public void attachPluginCfgSnapshot2Manifest(Manifest manifest) {
Map<String, Attributes> entries = manifest.getEntries();
StringBuffer globalPluginStore = new StringBuffer();
for (Map.Entry<String, Long> e : globalPluginStoreLastModify.entrySet()) {
globalPluginStore.append(e.getKey()).append(PluginMeta.NAME_VER_SPLIT).append(e.getValue()).append(",");
}
final Attributes pmetas = new Attributes();
pmetas.put(new Attributes.Name(KeyedPluginStore.PluginMetas.KEY_GLOBAL_PLUGIN_STORE), String.valueOf(globalPluginStore));
// 本次任务相关插件元信息
//KeyedPluginStore.PluginMetas pluginMetas = KeyedPluginStore.getAppAwarePluginMetas(false, collection
// .getName());
PluginManager pluginManager = TIS.get().getPluginManager();
Map<String, PluginWrapper> plugins = pluginManager.getActivePluginsMap();
// pluginManager.
final JSONArray jarray = new JSONArray();
this.pluginMetas.forEach((meta) -> {
meta.getLastModifyTimeStamp();
PluginWrapper plugin = plugins.get(meta.getPluginName());
if (plugin != null) {
Optional<PluginClassifier> classifier = plugin.getClassifier();
if (classifier.isPresent()) {
meta.setClassifier(classifier.get());
}
}
jarray.add(meta.toString());
});
// KeyedPluginStore.PluginMetas.KEY_PLUGIN_META
pmetas.put(new Attributes.Name(Config.KEY_PLUGIN_METAS), jarray.toJSONString());
pmetas.put(new Attributes.Name(KeyedPluginStore.PluginMetas.KEY_APP_LAST_MODIFY_TIMESTAMP),
String.valueOf(this.appLastModifyTimestamp));
entries.put(Config.KEY_PLUGIN_METAS, pmetas);
}
}