-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ClientStartNodeTask.java
179 lines (141 loc) · 5.65 KB
/
ClientStartNodeTask.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
/*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.client;
import org.apache.ignite.*;
import org.apache.ignite.compute.*;
import org.apache.ignite.configuration.*;
import org.apache.ignite.internal.util.typedef.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.resources.*;
import org.springframework.beans.factory.*;
import org.springframework.context.support.*;
import java.net.*;
import java.util.*;
import static org.apache.ignite.compute.ComputeJobResultPolicy.*;
/**
* Start node task, applicable arguments:
* <ul>
* <li>tcp</li>
* <li>http</li>
* <li>tcp+ssl</li>
* <li>http+ssl</li>
* </ul>
*/
public class ClientStartNodeTask extends TaskSingleJobSplitAdapter<String, Integer> {
/**
* Available node's configurations.
*/
private static final Map<String, String> NODE_CFG = new HashMap<String, String>() {{
put("tcp", "modules/clients/src/test/resources/spring-server-node.xml");
put("http", "modules/clients/src/test/resources/spring-server-node.xml");
put("tcp+ssl", "modules/clients/src/test/resources/spring-server-ssl-node.xml");
put("http+ssl", "modules/clients/src/test/resources/spring-server-ssl-node.xml");
}};
/** */
@LoggerResource
private transient IgniteLogger log;
/** */
@IgniteInstanceResource
private transient Ignite ignite;
/** {@inheritDoc} */
@Override protected Object executeJob(int gridSize, String type) {
log.info(">>> Starting new grid node [currGridSize=" + gridSize + ", arg=" + type + "]");
if (type == null)
throw new IllegalArgumentException("Node type to start should be specified.");
IgniteConfiguration cfg = getConfig(type);
// Generate unique for this VM grid name.
String gridName = cfg.getGridName() + " (" + UUID.randomUUID() + ")";
// Update grid name (required to be unique).
cfg.setGridName(gridName);
// Start new node in current VM.
Ignite g = G.start(cfg);
log.info(">>> Grid started [nodeId=" + g.cluster().localNode().id() + ", name='" + g.name() + "']");
return true;
}
/** {@inheritDoc} */
@Override public ComputeJobResultPolicy result(ComputeJobResult res, List<ComputeJobResult> rcvd) {
if (res.getException() != null)
return FAILOVER;
return WAIT;
}
/**
* Load grid configuration for specified node type.
*
* @param type Node type to load configuration for.
* @return Grid configuration for specified node type.
*/
static IgniteConfiguration getConfig(String type) {
String path = NODE_CFG.get(type);
if (path == null)
throw new IllegalArgumentException("Unsupported node type: " + type);
URL url = U.resolveGridGainUrl(path);
BeanFactory ctx = new FileSystemXmlApplicationContext(url.toString());
return (IgniteConfiguration)ctx.getBean("grid.cfg");
}
/**
* Example for start/stop node tasks.
*
* @param args Not used.
*/
public static void main(String[] args) {
String nodeType = "tcp+ssl";
// Start initial node = 1
try (Ignite g = G.start(NODE_CFG.get(nodeType))) {
// Change topology.
changeTopology(g, 4, 1, nodeType);
changeTopology(g, 1, 4, nodeType);
// Stop node by id = 0
g.compute().execute(ClientStopNodeTask.class, g.cluster().localNode().id().toString());
// Wait for node stops.
//U.sleep(1000);
assert G.allGrids().isEmpty();
}
catch (IgniteCheckedException e) {
System.err.println("Uncaught exception: " + e.getMessage());
e.printStackTrace(System.err);
}
}
/**
* Change topology.
*
* @param parent Grid to execute tasks on.
* @param add New nodes count.
* @param rmv Remove nodes count.
* @param type Type of nodes to manipulate.
* @throws IgniteCheckedException On any exception.
*/
private static void changeTopology(Ignite parent, int add, int rmv, String type) throws IgniteCheckedException {
Collection<ComputeTaskFuture<?>> tasks = new ArrayList<>();
IgniteCompute comp = parent.compute().withAsync();
// Start nodes in parallel.
while (add-- > 0) {
comp.execute(ClientStartNodeTask.class, type);
tasks.add(comp.future());
}
for (ComputeTaskFuture<?> task : tasks)
task.get();
// Stop nodes in sequence.
while (rmv-- > 0)
parent.compute().execute(ClientStopNodeTask.class, type);
// Wait for node stops.
//U.sleep(1000);
Collection<String> gridNames = new ArrayList<>();
for (Ignite g : G.allGrids())
gridNames.add(g.name());
parent.log().info(">>> Available grids: " + gridNames);
}
}