-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathClusterMetadataManifest.java
994 lines (861 loc) · 38 KB
/
ClusterMetadataManifest.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
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
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.gateway.remote;
import org.opensearch.Version;
import org.opensearch.core.ParseField;
import org.opensearch.core.common.Strings;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.core.xcontent.ConstructingObjectParser;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.ObjectParser;
import org.opensearch.core.xcontent.ToXContentFragment;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Manifest file which contains the details of the uploaded entity metadata
*
* @opensearch.internal
*/
public class ClusterMetadataManifest implements Writeable, ToXContentFragment {
public static final int CODEC_V0 = 0; // Older codec version, where we haven't introduced codec versions for manifest.
public static final int CODEC_V1 = 1; // In Codec V1 we have introduced global-metadata and codec version in Manifest file.
public static final int CODEC_V2 = 2; // In Codec V2, there are seperate metadata files rather than a single global metadata file.
public static final int CODEC_V3 = 3; // In Codec V3, we introduce index routing-metadata in manifest file.
private static final ParseField CLUSTER_TERM_FIELD = new ParseField("cluster_term");
private static final ParseField STATE_VERSION_FIELD = new ParseField("state_version");
private static final ParseField CLUSTER_UUID_FIELD = new ParseField("cluster_uuid");
private static final ParseField STATE_UUID_FIELD = new ParseField("state_uuid");
private static final ParseField OPENSEARCH_VERSION_FIELD = new ParseField("opensearch_version");
private static final ParseField NODE_ID_FIELD = new ParseField("node_id");
private static final ParseField COMMITTED_FIELD = new ParseField("committed");
private static final ParseField CODEC_VERSION_FIELD = new ParseField("codec_version");
private static final ParseField GLOBAL_METADATA_FIELD = new ParseField("global_metadata");
private static final ParseField INDICES_FIELD = new ParseField("indices");
private static final ParseField PREVIOUS_CLUSTER_UUID = new ParseField("previous_cluster_uuid");
private static final ParseField CLUSTER_UUID_COMMITTED = new ParseField("cluster_uuid_committed");
private static final ParseField UPLOADED_COORDINATOR_METADATA = new ParseField("uploaded_coordinator_metadata");
private static final ParseField UPLOADED_SETTINGS_METADATA = new ParseField("uploaded_settings_metadata");
private static final ParseField UPLOADED_TEMPLATES_METADATA = new ParseField("uploaded_templates_metadata");
private static final ParseField UPLOADED_CUSTOM_METADATA = new ParseField("uploaded_custom_metadata");
private static final ParseField ROUTING_TABLE_VERSION_FIELD = new ParseField("routing_table_version");
private static final ParseField INDICES_ROUTING_FIELD = new ParseField("indices_routing");
private static ClusterMetadataManifest.Builder manifestV0Builder(Object[] fields) {
return ClusterMetadataManifest.builder()
.clusterTerm(term(fields))
.stateVersion(version(fields))
.clusterUUID(clusterUUID(fields))
.stateUUID(stateUUID(fields))
.opensearchVersion(opensearchVersion(fields))
.nodeId(nodeId(fields))
.committed(committed(fields))
.codecVersion(CODEC_V0)
.indices(indices(fields))
.previousClusterUUID(previousClusterUUID(fields))
.clusterUUIDCommitted(clusterUUIDCommitted(fields));
}
private static ClusterMetadataManifest.Builder manifestV1Builder(Object[] fields) {
return manifestV0Builder(fields).codecVersion(codecVersion(fields)).globalMetadataFileName(globalMetadataFileName(fields));
}
private static ClusterMetadataManifest.Builder manifestV2Builder(Object[] fields) {
return manifestV0Builder(fields).codecVersion(codecVersion(fields))
.coordinationMetadata(coordinationMetadata(fields))
.settingMetadata(settingsMetadata(fields))
.templatesMetadata(templatesMetadata(fields))
.customMetadataMap(customMetadata(fields));
}
private static ClusterMetadataManifest.Builder manifestV3Builder(Object[] fields) {
return manifestV2Builder(fields).codecVersion(codecVersion(fields))
.routingTableVersion(routingTableVersion(fields))
.indicesRouting(indicesRouting(fields));
}
private static long term(Object[] fields) {
return (long) fields[0];
}
private static long version(Object[] fields) {
return (long) fields[1];
}
private static String clusterUUID(Object[] fields) {
return (String) fields[2];
}
private static String stateUUID(Object[] fields) {
return (String) fields[3];
}
private static Version opensearchVersion(Object[] fields) {
return Version.fromId((int) fields[4]);
}
private static String nodeId(Object[] fields) {
return (String) fields[5];
}
private static boolean committed(Object[] fields) {
return (boolean) fields[6];
}
private static List<UploadedIndexMetadata> indices(Object[] fields) {
return (List<UploadedIndexMetadata>) fields[7];
}
private static String previousClusterUUID(Object[] fields) {
return (String) fields[8];
}
private static boolean clusterUUIDCommitted(Object[] fields) {
return (boolean) fields[9];
}
private static int codecVersion(Object[] fields) {
return (int) fields[10];
}
private static String globalMetadataFileName(Object[] fields) {
return (String) fields[11];
}
private static UploadedMetadataAttribute coordinationMetadata(Object[] fields) {
return (UploadedMetadataAttribute) fields[11];
}
private static UploadedMetadataAttribute settingsMetadata(Object[] fields) {
return (UploadedMetadataAttribute) fields[12];
}
private static UploadedMetadataAttribute templatesMetadata(Object[] fields) {
return (UploadedMetadataAttribute) fields[13];
}
private static Map<String, UploadedMetadataAttribute> customMetadata(Object[] fields) {
List<UploadedMetadataAttribute> customs = (List<UploadedMetadataAttribute>) fields[14];
return customs.stream().collect(Collectors.toMap(UploadedMetadataAttribute::getAttributeName, Function.identity()));
}
private static long routingTableVersion(Object[] fields) {
return (long) fields[15];
}
private static List<UploadedIndexMetadata> indicesRouting(Object[] fields) {
return (List<UploadedIndexMetadata>) fields[16];
}
private static final ConstructingObjectParser<ClusterMetadataManifest, Void> PARSER_V0 = new ConstructingObjectParser<>(
"cluster_metadata_manifest",
fields -> manifestV0Builder(fields).build()
);
private static final ConstructingObjectParser<ClusterMetadataManifest, Void> PARSER_V1 = new ConstructingObjectParser<>(
"cluster_metadata_manifest",
fields -> manifestV1Builder(fields).build()
);
private static final ConstructingObjectParser<ClusterMetadataManifest, Void> PARSER_V2 = new ConstructingObjectParser<>(
"cluster_metadata_manifest",
fields -> manifestV2Builder(fields).build()
);
private static final ConstructingObjectParser<ClusterMetadataManifest, Void> PARSER_V3 = new ConstructingObjectParser<>(
"cluster_metadata_manifest",
fields -> manifestV3Builder(fields).build()
);
private static final ConstructingObjectParser<ClusterMetadataManifest, Void> CURRENT_PARSER = PARSER_V3;
static {
declareParser(PARSER_V0, CODEC_V0);
declareParser(PARSER_V1, CODEC_V1);
declareParser(PARSER_V2, CODEC_V2);
declareParser(PARSER_V3, CODEC_V3);
}
private static void declareParser(ConstructingObjectParser<ClusterMetadataManifest, Void> parser, long codec_version) {
parser.declareLong(ConstructingObjectParser.constructorArg(), CLUSTER_TERM_FIELD);
parser.declareLong(ConstructingObjectParser.constructorArg(), STATE_VERSION_FIELD);
parser.declareString(ConstructingObjectParser.constructorArg(), CLUSTER_UUID_FIELD);
parser.declareString(ConstructingObjectParser.constructorArg(), STATE_UUID_FIELD);
parser.declareInt(ConstructingObjectParser.constructorArg(), OPENSEARCH_VERSION_FIELD);
parser.declareString(ConstructingObjectParser.constructorArg(), NODE_ID_FIELD);
parser.declareBoolean(ConstructingObjectParser.constructorArg(), COMMITTED_FIELD);
parser.declareObjectArray(
ConstructingObjectParser.constructorArg(),
(p, c) -> UploadedIndexMetadata.fromXContent(p),
INDICES_FIELD
);
parser.declareString(ConstructingObjectParser.constructorArg(), PREVIOUS_CLUSTER_UUID);
parser.declareBoolean(ConstructingObjectParser.constructorArg(), CLUSTER_UUID_COMMITTED);
if (codec_version == CODEC_V1) {
parser.declareInt(ConstructingObjectParser.constructorArg(), CODEC_VERSION_FIELD);
parser.declareString(ConstructingObjectParser.constructorArg(), GLOBAL_METADATA_FIELD);
} else if (codec_version >= CODEC_V2) {
parser.declareInt(ConstructingObjectParser.constructorArg(), CODEC_VERSION_FIELD);
parser.declareNamedObject(
ConstructingObjectParser.optionalConstructorArg(),
UploadedMetadataAttribute.PARSER,
UPLOADED_COORDINATOR_METADATA
);
parser.declareNamedObject(
ConstructingObjectParser.optionalConstructorArg(),
UploadedMetadataAttribute.PARSER,
UPLOADED_SETTINGS_METADATA
);
parser.declareNamedObject(
ConstructingObjectParser.optionalConstructorArg(),
UploadedMetadataAttribute.PARSER,
UPLOADED_TEMPLATES_METADATA
);
parser.declareNamedObjects(
ConstructingObjectParser.optionalConstructorArg(),
UploadedMetadataAttribute.PARSER,
UPLOADED_CUSTOM_METADATA
);
}
if (codec_version >= CODEC_V3) {
parser.declareLong(ConstructingObjectParser.constructorArg(), ROUTING_TABLE_VERSION_FIELD);
parser.declareObjectArray(
ConstructingObjectParser.constructorArg(),
(p, c) -> UploadedIndexMetadata.fromXContent(p),
INDICES_ROUTING_FIELD
);
}
}
private final int codecVersion;
private final String globalMetadataFileName;
private final UploadedMetadataAttribute uploadedCoordinationMetadata;
private final UploadedMetadataAttribute uploadedSettingsMetadata;
private final UploadedMetadataAttribute uploadedTemplatesMetadata;
private final Map<String, UploadedMetadataAttribute> uploadedCustomMetadataMap;
private final List<UploadedIndexMetadata> indices;
private final long clusterTerm;
private final long stateVersion;
private final String clusterUUID;
private final String stateUUID;
private final Version opensearchVersion;
private final String nodeId;
private final boolean committed;
private final String previousClusterUUID;
private final boolean clusterUUIDCommitted;
private final long routingTableVersion;
private final List<UploadedIndexMetadata> indicesRouting;
public List<UploadedIndexMetadata> getIndices() {
return indices;
}
public long getClusterTerm() {
return clusterTerm;
}
public long getStateVersion() {
return stateVersion;
}
public String getClusterUUID() {
return clusterUUID;
}
public String getStateUUID() {
return stateUUID;
}
public Version getOpensearchVersion() {
return opensearchVersion;
}
public String getNodeId() {
return nodeId;
}
public boolean isCommitted() {
return committed;
}
public String getPreviousClusterUUID() {
return previousClusterUUID;
}
public boolean isClusterUUIDCommitted() {
return clusterUUIDCommitted;
}
public int getCodecVersion() {
return codecVersion;
}
public String getGlobalMetadataFileName() {
return globalMetadataFileName;
}
public UploadedMetadataAttribute getCoordinationMetadata() {
return uploadedCoordinationMetadata;
}
public UploadedMetadataAttribute getSettingsMetadata() {
return uploadedSettingsMetadata;
}
public UploadedMetadataAttribute getTemplatesMetadata() {
return uploadedTemplatesMetadata;
}
public Map<String, UploadedMetadataAttribute> getCustomMetadataMap() {
return uploadedCustomMetadataMap;
}
public boolean hasMetadataAttributesFiles() {
return uploadedCoordinationMetadata != null
|| uploadedSettingsMetadata != null
|| uploadedTemplatesMetadata != null
|| !uploadedCustomMetadataMap.isEmpty();
}
public long getRoutingTableVersion() {
return routingTableVersion;
}
public List<UploadedIndexMetadata> getIndicesRouting() {
return indicesRouting;
}
public ClusterMetadataManifest(
long clusterTerm,
long version,
String clusterUUID,
String stateUUID,
Version opensearchVersion,
String nodeId,
boolean committed,
int codecVersion,
String globalMetadataFileName,
List<UploadedIndexMetadata> indices,
String previousClusterUUID,
boolean clusterUUIDCommitted,
UploadedMetadataAttribute uploadedCoordinationMetadata,
UploadedMetadataAttribute uploadedSettingsMetadata,
UploadedMetadataAttribute uploadedTemplatesMetadata,
Map<String, UploadedMetadataAttribute> uploadedCustomMetadataMap,
long routingTableVersion,
List<UploadedIndexMetadata> indicesRouting
) {
this.clusterTerm = clusterTerm;
this.stateVersion = version;
this.clusterUUID = clusterUUID;
this.stateUUID = stateUUID;
this.opensearchVersion = opensearchVersion;
this.nodeId = nodeId;
this.committed = committed;
this.codecVersion = codecVersion;
this.globalMetadataFileName = globalMetadataFileName;
this.indices = Collections.unmodifiableList(indices);
this.previousClusterUUID = previousClusterUUID;
this.clusterUUIDCommitted = clusterUUIDCommitted;
this.routingTableVersion = routingTableVersion;
this.indicesRouting = Collections.unmodifiableList(indicesRouting);
this.uploadedCoordinationMetadata = uploadedCoordinationMetadata;
this.uploadedSettingsMetadata = uploadedSettingsMetadata;
this.uploadedTemplatesMetadata = uploadedTemplatesMetadata;
this.uploadedCustomMetadataMap = Collections.unmodifiableMap(
uploadedCustomMetadataMap != null ? uploadedCustomMetadataMap : new HashMap<>()
);
}
public ClusterMetadataManifest(StreamInput in) throws IOException {
this.clusterTerm = in.readVLong();
this.stateVersion = in.readVLong();
this.clusterUUID = in.readString();
this.stateUUID = in.readString();
this.opensearchVersion = Version.fromId(in.readInt());
this.nodeId = in.readString();
this.committed = in.readBoolean();
this.indices = Collections.unmodifiableList(in.readList(UploadedIndexMetadata::new));
this.previousClusterUUID = in.readString();
this.clusterUUIDCommitted = in.readBoolean();
if (in.getVersion().onOrAfter(Version.V_3_0_0)) {
this.codecVersion = in.readInt();
this.uploadedCoordinationMetadata = new UploadedMetadataAttribute(in);
this.uploadedSettingsMetadata = new UploadedMetadataAttribute(in);
this.uploadedTemplatesMetadata = new UploadedMetadataAttribute(in);
this.uploadedCustomMetadataMap = Collections.unmodifiableMap(
in.readMap(StreamInput::readString, UploadedMetadataAttribute::new)
);
this.globalMetadataFileName = null;
this.routingTableVersion = in.readLong();
this.indicesRouting = Collections.unmodifiableList(in.readList(UploadedIndexMetadata::new));
} else if (in.getVersion().onOrAfter(Version.V_2_12_0)) {
this.codecVersion = in.readInt();
this.globalMetadataFileName = in.readString();
this.uploadedCoordinationMetadata = null;
this.uploadedSettingsMetadata = null;
this.uploadedTemplatesMetadata = null;
this.uploadedCustomMetadataMap = null;
this.routingTableVersion = -1;
this.indicesRouting = null;
} else {
this.codecVersion = CODEC_V0; // Default codec
this.globalMetadataFileName = null;
this.uploadedCoordinationMetadata = null;
this.uploadedSettingsMetadata = null;
this.uploadedTemplatesMetadata = null;
this.uploadedCustomMetadataMap = null;
this.routingTableVersion = -1;
this.indicesRouting = null;
}
}
public static Builder builder() {
return new Builder();
}
public static Builder builder(ClusterMetadataManifest manifest) {
return new Builder(manifest);
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field(CLUSTER_TERM_FIELD.getPreferredName(), getClusterTerm())
.field(STATE_VERSION_FIELD.getPreferredName(), getStateVersion())
.field(CLUSTER_UUID_FIELD.getPreferredName(), getClusterUUID())
.field(STATE_UUID_FIELD.getPreferredName(), getStateUUID())
.field(OPENSEARCH_VERSION_FIELD.getPreferredName(), getOpensearchVersion().id)
.field(NODE_ID_FIELD.getPreferredName(), getNodeId())
.field(COMMITTED_FIELD.getPreferredName(), isCommitted());
builder.startArray(INDICES_FIELD.getPreferredName());
{
for (UploadedIndexMetadata uploadedIndexMetadata : indices) {
builder.startObject();
uploadedIndexMetadata.toXContent(builder, params);
builder.endObject();
}
}
builder.endArray();
builder.field(PREVIOUS_CLUSTER_UUID.getPreferredName(), getPreviousClusterUUID());
builder.field(CLUSTER_UUID_COMMITTED.getPreferredName(), isClusterUUIDCommitted());
if (onOrAfterCodecVersion(CODEC_V2)) {
builder.field(CODEC_VERSION_FIELD.getPreferredName(), getCodecVersion());
if (getCoordinationMetadata() != null) {
builder.startObject(UPLOADED_COORDINATOR_METADATA.getPreferredName());
getCoordinationMetadata().toXContent(builder, params);
builder.endObject();
}
if (getSettingsMetadata() != null) {
builder.startObject(UPLOADED_SETTINGS_METADATA.getPreferredName());
getSettingsMetadata().toXContent(builder, params);
builder.endObject();
}
if (getTemplatesMetadata() != null) {
builder.startObject(UPLOADED_TEMPLATES_METADATA.getPreferredName());
getTemplatesMetadata().toXContent(builder, params);
builder.endObject();
}
builder.startObject(UPLOADED_CUSTOM_METADATA.getPreferredName());
for (UploadedMetadataAttribute attribute : getCustomMetadataMap().values()) {
attribute.toXContent(builder, params);
}
builder.endObject();
} else if (onOrAfterCodecVersion(CODEC_V1)) {
builder.field(CODEC_VERSION_FIELD.getPreferredName(), getCodecVersion());
builder.field(GLOBAL_METADATA_FIELD.getPreferredName(), getGlobalMetadataFileName());
}
if (onOrAfterCodecVersion(CODEC_V3)) {
builder.field(ROUTING_TABLE_VERSION_FIELD.getPreferredName(), getRoutingTableVersion());
builder.startArray(INDICES_ROUTING_FIELD.getPreferredName());
{
for (UploadedIndexMetadata uploadedIndexMetadata : indicesRouting) {
builder.startObject();
uploadedIndexMetadata.toXContent(builder, params);
builder.endObject();
}
}
builder.endArray();
}
return builder;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(clusterTerm);
out.writeVLong(stateVersion);
out.writeString(clusterUUID);
out.writeString(stateUUID);
out.writeInt(opensearchVersion.id);
out.writeString(nodeId);
out.writeBoolean(committed);
out.writeCollection(indices);
out.writeString(previousClusterUUID);
out.writeBoolean(clusterUUIDCommitted);
if (out.getVersion().onOrAfter(Version.V_3_0_0)) {
out.writeInt(codecVersion);
uploadedCoordinationMetadata.writeTo(out);
uploadedSettingsMetadata.writeTo(out);
uploadedTemplatesMetadata.writeTo(out);
out.writeMap(uploadedCustomMetadataMap, StreamOutput::writeString, (o, v) -> v.writeTo(o));
out.writeLong(routingTableVersion);
out.writeCollection(indicesRouting);
} else if (out.getVersion().onOrAfter(Version.V_2_12_0)) {
out.writeInt(codecVersion);
out.writeString(globalMetadataFileName);
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ClusterMetadataManifest that = (ClusterMetadataManifest) o;
return Objects.equals(indices, that.indices)
&& clusterTerm == that.clusterTerm
&& stateVersion == that.stateVersion
&& Objects.equals(clusterUUID, that.clusterUUID)
&& Objects.equals(stateUUID, that.stateUUID)
&& Objects.equals(opensearchVersion, that.opensearchVersion)
&& Objects.equals(nodeId, that.nodeId)
&& Objects.equals(committed, that.committed)
&& Objects.equals(previousClusterUUID, that.previousClusterUUID)
&& Objects.equals(clusterUUIDCommitted, that.clusterUUIDCommitted)
&& Objects.equals(globalMetadataFileName, that.globalMetadataFileName)
&& Objects.equals(codecVersion, that.codecVersion)
&& Objects.equals(routingTableVersion, that.routingTableVersion)
&& Objects.equals(indicesRouting, that.indicesRouting);
}
@Override
public int hashCode() {
return Objects.hash(
codecVersion,
globalMetadataFileName,
indices,
clusterTerm,
stateVersion,
clusterUUID,
stateUUID,
opensearchVersion,
nodeId,
committed,
previousClusterUUID,
clusterUUIDCommitted,
routingTableVersion,
indicesRouting
);
}
@Override
public String toString() {
return Strings.toString(MediaTypeRegistry.JSON, this);
}
public boolean onOrAfterCodecVersion(int codecVersion) {
return this.codecVersion >= codecVersion;
}
public static ClusterMetadataManifest fromXContentV0(XContentParser parser) throws IOException {
return PARSER_V0.parse(parser, null);
}
public static ClusterMetadataManifest fromXContentV1(XContentParser parser) throws IOException {
return PARSER_V1.parse(parser, null);
}
public static ClusterMetadataManifest fromXContentV2(XContentParser parser) throws IOException {
return PARSER_V2.parse(parser, null);
}
public static ClusterMetadataManifest fromXContent(XContentParser parser) throws IOException {
return CURRENT_PARSER.parse(parser, null);
}
/**
* Builder for ClusterMetadataManifest
*
* @opensearch.internal
*/
public static class Builder {
private String globalMetadataFileName;
private UploadedMetadataAttribute coordinationMetadata;
private UploadedMetadataAttribute settingsMetadata;
private UploadedMetadataAttribute templatesMetadata;
private Map<String, UploadedMetadataAttribute> customMetadataMap;
private int codecVersion;
private List<UploadedIndexMetadata> indices;
private long clusterTerm;
private long stateVersion;
private String clusterUUID;
private String stateUUID;
private Version opensearchVersion;
private String nodeId;
private String previousClusterUUID;
private boolean committed;
private boolean clusterUUIDCommitted;
private long routingTableVersion;
private List<UploadedIndexMetadata> indicesRouting;
public Builder indices(List<UploadedIndexMetadata> indices) {
this.indices = indices;
return this;
}
public Builder routingTableVersion(long routingTableVersion) {
this.routingTableVersion = routingTableVersion;
return this;
}
public Builder indicesRouting(List<UploadedIndexMetadata> indicesRouting) {
this.indicesRouting = indicesRouting;
return this;
}
public Builder codecVersion(int codecVersion) {
this.codecVersion = codecVersion;
return this;
}
public Builder globalMetadataFileName(String globalMetadataFileName) {
this.globalMetadataFileName = globalMetadataFileName;
return this;
}
public Builder coordinationMetadata(UploadedMetadataAttribute coordinationMetadata) {
this.coordinationMetadata = coordinationMetadata;
return this;
}
public Builder settingMetadata(UploadedMetadataAttribute settingsMetadata) {
this.settingsMetadata = settingsMetadata;
return this;
}
public Builder templatesMetadata(UploadedMetadataAttribute templatesMetadata) {
this.templatesMetadata = templatesMetadata;
return this;
}
public Builder customMetadataMap(Map<String, UploadedMetadataAttribute> customMetadataMap) {
this.customMetadataMap = customMetadataMap;
return this;
}
public Builder put(String custom, UploadedMetadataAttribute customMetadata) {
this.customMetadataMap.put(custom, customMetadata);
return this;
}
public Builder clusterTerm(long clusterTerm) {
this.clusterTerm = clusterTerm;
return this;
}
public Builder stateVersion(long stateVersion) {
this.stateVersion = stateVersion;
return this;
}
public Builder clusterUUID(String clusterUUID) {
this.clusterUUID = clusterUUID;
return this;
}
public Builder stateUUID(String stateUUID) {
this.stateUUID = stateUUID;
return this;
}
public Builder opensearchVersion(Version opensearchVersion) {
this.opensearchVersion = opensearchVersion;
return this;
}
public Builder nodeId(String nodeId) {
this.nodeId = nodeId;
return this;
}
public Builder committed(boolean committed) {
this.committed = committed;
return this;
}
public List<UploadedIndexMetadata> getIndices() {
return indices;
}
public List<UploadedIndexMetadata> getIndicesRouting() {
return indicesRouting;
}
public Builder previousClusterUUID(String previousClusterUUID) {
this.previousClusterUUID = previousClusterUUID;
return this;
}
public Builder clusterUUIDCommitted(boolean clusterUUIDCommitted) {
this.clusterUUIDCommitted = clusterUUIDCommitted;
return this;
}
public Builder() {
indices = new ArrayList<>();
customMetadataMap = new HashMap<>();
indicesRouting = new ArrayList<>();
}
public Builder(ClusterMetadataManifest manifest) {
this.clusterTerm = manifest.clusterTerm;
this.stateVersion = manifest.stateVersion;
this.clusterUUID = manifest.clusterUUID;
this.stateUUID = manifest.stateUUID;
this.opensearchVersion = manifest.opensearchVersion;
this.nodeId = manifest.nodeId;
this.committed = manifest.committed;
this.globalMetadataFileName = manifest.globalMetadataFileName;
this.coordinationMetadata = manifest.uploadedCoordinationMetadata;
this.settingsMetadata = manifest.uploadedSettingsMetadata;
this.templatesMetadata = manifest.uploadedTemplatesMetadata;
this.customMetadataMap = manifest.uploadedCustomMetadataMap;
this.codecVersion = manifest.codecVersion;
this.indices = new ArrayList<>(manifest.indices);
this.previousClusterUUID = manifest.previousClusterUUID;
this.clusterUUIDCommitted = manifest.clusterUUIDCommitted;
this.routingTableVersion = manifest.routingTableVersion;
this.indicesRouting = new ArrayList<>(manifest.indicesRouting);
}
public ClusterMetadataManifest build() {
return new ClusterMetadataManifest(
clusterTerm,
stateVersion,
clusterUUID,
stateUUID,
opensearchVersion,
nodeId,
committed,
codecVersion,
globalMetadataFileName,
indices,
previousClusterUUID,
clusterUUIDCommitted,
coordinationMetadata,
settingsMetadata,
templatesMetadata,
customMetadataMap,
routingTableVersion,
indicesRouting
);
}
}
/**
* Interface representing uploaded metadata
*/
public interface UploadedMetadata {
/**
* Gets the component or part of the system this upload belongs to.
*
* @return A string identifying the component
*/
String getComponent();
/**
* Gets the name of the file that was uploaded
*
* @return The name of the uploaded file as a string
*/
String getUploadedFilename();
}
/**
* Metadata for uploaded index metadata
*
* @opensearch.internal
*/
public static class UploadedIndexMetadata implements UploadedMetadata, Writeable, ToXContentFragment {
private static final ParseField INDEX_NAME_FIELD = new ParseField("index_name");
private static final ParseField INDEX_UUID_FIELD = new ParseField("index_uuid");
private static final ParseField UPLOADED_FILENAME_FIELD = new ParseField("uploaded_filename");
private static String indexName(Object[] fields) {
return (String) fields[0];
}
private static String indexUUID(Object[] fields) {
return (String) fields[1];
}
private static String uploadedFilename(Object[] fields) {
return (String) fields[2];
}
private static final ConstructingObjectParser<UploadedIndexMetadata, Void> PARSER = new ConstructingObjectParser<>(
"uploaded_index_metadata",
fields -> new UploadedIndexMetadata(indexName(fields), indexUUID(fields), uploadedFilename(fields))
);
static {
PARSER.declareString(ConstructingObjectParser.constructorArg(), INDEX_NAME_FIELD);
PARSER.declareString(ConstructingObjectParser.constructorArg(), INDEX_UUID_FIELD);
PARSER.declareString(ConstructingObjectParser.constructorArg(), UPLOADED_FILENAME_FIELD);
}
static final String COMPONENT_PREFIX = "index--";
private final String indexName;
private final String indexUUID;
private final String uploadedFilename;
public UploadedIndexMetadata(String indexName, String indexUUID, String uploadedFileName) {
this.indexName = indexName;
this.indexUUID = indexUUID;
this.uploadedFilename = uploadedFileName;
}
public UploadedIndexMetadata(StreamInput in) throws IOException {
this.indexName = in.readString();
this.indexUUID = in.readString();
this.uploadedFilename = in.readString();
}
public String getUploadedFilePath() {
return uploadedFilename;
}
@Override
public String getComponent() {
return COMPONENT_PREFIX + getIndexName();
}
public String getUploadedFilename() {
String[] splitPath = uploadedFilename.split("/");
return splitPath[splitPath.length - 1];
}
public String getIndexName() {
return indexName;
}
public String getIndexUUID() {
return indexUUID;
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.field(INDEX_NAME_FIELD.getPreferredName(), getIndexName())
.field(INDEX_UUID_FIELD.getPreferredName(), getIndexUUID())
.field(UPLOADED_FILENAME_FIELD.getPreferredName(), getUploadedFilePath());
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(indexName);
out.writeString(indexUUID);
out.writeString(uploadedFilename);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final UploadedIndexMetadata that = (UploadedIndexMetadata) o;
return Objects.equals(indexName, that.indexName)
&& Objects.equals(indexUUID, that.indexUUID)
&& Objects.equals(uploadedFilename, that.uploadedFilename);
}
@Override
public int hashCode() {
return Objects.hash(indexName, indexUUID, uploadedFilename);
}
@Override
public String toString() {
return Strings.toString(MediaTypeRegistry.JSON, this);
}
public static UploadedIndexMetadata fromXContent(XContentParser parser) throws IOException {
return PARSER.parse(parser, null);
}
}
/**
* Metadata for uploaded metadata attribute
*
* @opensearch.internal
*/
public static class UploadedMetadataAttribute implements UploadedMetadata, Writeable, ToXContentFragment {
private static final ParseField UPLOADED_FILENAME_FIELD = new ParseField("uploaded_filename");
private static final ObjectParser.NamedObjectParser<UploadedMetadataAttribute, Void> PARSER;
static {
ConstructingObjectParser<UploadedMetadataAttribute, String> innerParser = new ConstructingObjectParser<>(
"uploaded_metadata_attribute",
true,
(Object[] parsedObject, String name) -> {
String uploadedFilename = (String) parsedObject[0];
return new UploadedMetadataAttribute(name, uploadedFilename);
}
);
innerParser.declareString(ConstructingObjectParser.constructorArg(), UPLOADED_FILENAME_FIELD);
PARSER = ((p, c, name) -> innerParser.parse(p, name));
}
private final String attributeName;
private final String uploadedFilename;
public UploadedMetadataAttribute(String attributeName, String uploadedFilename) {
this.attributeName = attributeName;
this.uploadedFilename = uploadedFilename;
}
public UploadedMetadataAttribute(StreamInput in) throws IOException {
this.attributeName = in.readString();
this.uploadedFilename = in.readString();
}
public String getAttributeName() {
return attributeName;
}
@Override
public String getComponent() {
return getAttributeName();
}
public String getUploadedFilename() {
return uploadedFilename;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(attributeName);
out.writeString(uploadedFilename);
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.startObject(getAttributeName())
.field(UPLOADED_FILENAME_FIELD.getPreferredName(), getUploadedFilename())
.endObject();
}
public static UploadedMetadataAttribute fromXContent(XContentParser parser) throws IOException {
return PARSER.parse(parser, null, parser.currentName());
}
@Override
public String toString() {
return "UploadedMetadataAttribute{"
+ "attributeName='"
+ attributeName
+ '\''
+ ", uploadedFilename='"
+ uploadedFilename
+ '\''
+ '}';
}
}
}