19
19
20
20
package org .elasticsearch .action .admin .cluster .repositories .verify ;
21
21
22
+ import org .elasticsearch .Version ;
22
23
import org .elasticsearch .action .ActionResponse ;
23
24
import org .elasticsearch .cluster .ClusterName ;
24
25
import org .elasticsearch .cluster .node .DiscoveryNode ;
26
+ import org .elasticsearch .common .ParseField ;
25
27
import org .elasticsearch .common .Strings ;
26
28
import org .elasticsearch .common .io .stream .StreamInput ;
27
29
import org .elasticsearch .common .io .stream .StreamOutput ;
30
+ import org .elasticsearch .common .io .stream .Writeable ;
31
+ import org .elasticsearch .common .transport .TransportAddress ;
32
+ import org .elasticsearch .common .xcontent .ObjectParser ;
28
33
import org .elasticsearch .common .xcontent .ToXContentObject ;
29
34
import org .elasticsearch .common .xcontent .XContentBuilder ;
30
35
31
36
import java .io .IOException ;
37
+ import java .util .Arrays ;
38
+ import java .util .Collections ;
39
+ import java .util .List ;
40
+ import java .util .Objects ;
41
+ import java .util .stream .Collectors ;
32
42
33
43
/**
34
- * Unregister repository response
44
+ * Verify repository response
35
45
*/
36
46
public class VerifyRepositoryResponse extends ActionResponse implements ToXContentObject {
37
47
38
- private DiscoveryNode [] nodes ;
48
+ static final String NODES = "nodes" ;
49
+ static final String NAME = "name" ;
50
+
51
+ public static class NodeView implements Writeable , ToXContentObject {
52
+ private static final ObjectParser .NamedObjectParser <NodeView , Void > PARSER ;
53
+ static {
54
+ ObjectParser <NodeView , Void > internalParser = new ObjectParser <>(NODES );
55
+ internalParser .declareString (NodeView ::setName , new ParseField (NAME ));
56
+ PARSER = (p , v , name ) -> internalParser .parse (p , new NodeView (name ), null );
57
+ }
58
+
59
+ final String nodeId ;
60
+ String name ;
61
+
62
+ public NodeView (String nodeId ) { this .nodeId = nodeId ; }
63
+
64
+ public NodeView (String nodeId , String name ) {
65
+ this (nodeId );
66
+ this .name = name ;
67
+ }
68
+
69
+ public NodeView (StreamInput in ) throws IOException {
70
+ this (in .readString (), in .readString ());
71
+ }
72
+
73
+ @ Override
74
+ public void writeTo (StreamOutput out ) throws IOException {
75
+ out .writeString (nodeId );
76
+ out .writeString (name );
77
+ }
78
+
79
+ void setName (String name ) { this .name = name ; }
80
+
81
+ public String getName () { return name ; }
82
+
83
+ public String getNodeId () { return nodeId ; }
84
+
85
+ public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
86
+ builder .startObject (nodeId );
87
+ {
88
+ builder .field (NAME , name );
89
+ }
90
+ builder .endObject ();
91
+ return builder ;
92
+ }
93
+
94
+ /**
95
+ * Temporary method that allows turning a {@link NodeView} into a {@link DiscoveryNode}. This representation will never be used in
96
+ * practice, because in >= 6.4 a consumer of the response will only be able to retrieve a representation of {@link NodeView}
97
+ * objects.
98
+ *
99
+ * Effectively this will be used to hold the state of the object in 6.x so there is no need to have 2 backing objects that
100
+ * represent the state of the Response. In practice these will always be read by a consumer as a NodeView, but it eases the
101
+ * transition to master which will not contain any representation of a {@link DiscoveryNode}.
102
+ */
103
+ DiscoveryNode convertToDiscoveryNode () {
104
+ return new DiscoveryNode (name , nodeId , "" , "" , "" , new TransportAddress (TransportAddress .META_ADDRESS , 0 ),
105
+ Collections .emptyMap (), Collections .emptySet (), Version .CURRENT );
106
+ }
107
+
108
+ @ Override
109
+ public boolean equals (Object obj ) {
110
+ if (obj == null ) {
111
+ return false ;
112
+ }
113
+ if (getClass () != obj .getClass ()) {
114
+ return false ;
115
+ }
116
+ NodeView other = (NodeView ) obj ;
117
+ return Objects .equals (nodeId , other .nodeId ) &&
118
+ Objects .equals (name , other .name );
119
+ }
120
+
121
+ @ Override
122
+ public int hashCode () {
123
+ return Objects .hash (nodeId , name );
124
+ }
125
+ }
126
+
127
+ private List <DiscoveryNode > nodes ;
39
128
40
129
private ClusterName clusterName ;
41
130
@@ -45,53 +134,56 @@ public class VerifyRepositoryResponse extends ActionResponse implements ToXConte
45
134
46
135
public VerifyRepositoryResponse (ClusterName clusterName , DiscoveryNode [] nodes ) {
47
136
this .clusterName = clusterName ;
48
- this .nodes = nodes ;
137
+ this .nodes = Arrays . asList ( nodes ) ;
49
138
}
50
139
51
140
@ Override
52
141
public void readFrom (StreamInput in ) throws IOException {
53
142
super .readFrom (in );
54
- clusterName = new ClusterName (in );
55
- nodes = new DiscoveryNode [in .readVInt ()];
56
- for (int i =0 ; i <nodes .length ; i ++){
57
- nodes [i ] = new DiscoveryNode (in );
143
+ if (in .getVersion ().onOrAfter (Version .V_6_4_0 )) {
144
+ this .nodes = in .readList (NodeView ::new ).stream ().map (n -> n .convertToDiscoveryNode ()).collect (Collectors .toList ());
145
+ } else {
146
+ clusterName = new ClusterName (in );
147
+ this .nodes = in .readList (DiscoveryNode ::new );
58
148
}
59
149
}
60
150
61
151
@ Override
62
152
public void writeTo (StreamOutput out ) throws IOException {
63
153
super .writeTo (out );
64
- clusterName .writeTo (out );
65
- out .writeVInt (nodes .length );
66
- for (DiscoveryNode node : nodes ) {
67
- node .writeTo (out );
154
+ if (Version .CURRENT .onOrAfter (Version .V_6_4_0 )) {
155
+ out .writeList (getNodes ());
156
+ } else {
157
+ clusterName .writeTo (out );
158
+ out .writeList (nodes );
68
159
}
69
160
}
70
161
71
- public DiscoveryNode [] getNodes () {
72
- return nodes ;
162
+ public List < NodeView > getNodes () {
163
+ return nodes . stream (). map ( dn -> new NodeView ( dn . getId (), dn . getName ())). collect ( Collectors . toList ()) ;
73
164
}
74
165
75
166
public ClusterName getClusterName () {
76
167
return clusterName ;
77
168
}
78
169
79
- static final class Fields {
80
- static final String NODES = "nodes" ;
81
- static final String NAME = "name" ;
82
- }
83
-
84
170
@ Override
85
171
public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
86
172
builder .startObject ();
87
- builder .startObject (Fields .NODES );
88
- for (DiscoveryNode node : nodes ) {
89
- builder .startObject (node .getId ());
90
- builder .field (Fields .NAME , node .getName ());
173
+ {
174
+ builder .startObject (NODES );
175
+ {
176
+ for (DiscoveryNode node : nodes ) {
177
+ builder .startObject (node .getId ());
178
+ {
179
+ builder .field (NAME , node .getName ());
180
+ }
181
+ builder .endObject ();
182
+ }
183
+ }
91
184
builder .endObject ();
92
185
}
93
186
builder .endObject ();
94
- builder .endObject ();
95
187
return builder ;
96
188
}
97
189
0 commit comments