/
IgniteImpl.java
554 lines (464 loc) · 20.3 KB
/
IgniteImpl.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
/*
* 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.internal.app;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgnitionManager;
import org.apache.ignite.client.handler.ClientHandlerModule;
import org.apache.ignite.configuration.schemas.network.NetworkConfiguration;
import org.apache.ignite.configuration.schemas.store.DataStorageConfiguration;
import org.apache.ignite.configuration.schemas.table.TablesConfiguration;
import org.apache.ignite.internal.baseline.BaselineManager;
import org.apache.ignite.internal.configuration.ConfigurationManager;
import org.apache.ignite.internal.configuration.ConfigurationModule;
import org.apache.ignite.internal.configuration.ConfigurationModules;
import org.apache.ignite.internal.configuration.ConfigurationRegistry;
import org.apache.ignite.internal.configuration.ServiceLoaderModulesProvider;
import org.apache.ignite.internal.configuration.storage.ConfigurationStorage;
import org.apache.ignite.internal.configuration.storage.DistributedConfigurationStorage;
import org.apache.ignite.internal.configuration.storage.LocalConfigurationStorage;
import org.apache.ignite.internal.manager.IgniteComponent;
import org.apache.ignite.internal.metastorage.MetaStorageManager;
import org.apache.ignite.internal.metastorage.server.persistence.RocksDbKeyValueStorage;
import org.apache.ignite.internal.raft.Loza;
import org.apache.ignite.internal.rest.RestModule;
import org.apache.ignite.internal.sql.engine.QueryProcessor;
import org.apache.ignite.internal.sql.engine.SqlQueryProcessor;
import org.apache.ignite.internal.sql.engine.message.SqlQueryMessagesSerializationRegistryInitializer;
import org.apache.ignite.internal.table.distributed.TableManager;
import org.apache.ignite.internal.table.distributed.TableTxManagerImpl;
import org.apache.ignite.internal.tx.TxManager;
import org.apache.ignite.internal.tx.impl.HeapLockManager;
import org.apache.ignite.internal.tx.impl.IgniteTransactionsImpl;
import org.apache.ignite.internal.tx.message.TxMessagesSerializationRegistryInitializer;
import org.apache.ignite.internal.vault.VaultManager;
import org.apache.ignite.internal.vault.VaultService;
import org.apache.ignite.internal.vault.persistence.PersistentVaultService;
import org.apache.ignite.lang.IgniteException;
import org.apache.ignite.lang.IgniteInternalException;
import org.apache.ignite.lang.IgniteLogger;
import org.apache.ignite.lang.NodeStoppingException;
import org.apache.ignite.network.ClusterLocalConfiguration;
import org.apache.ignite.network.ClusterService;
import org.apache.ignite.network.MessageSerializationRegistryImpl;
import org.apache.ignite.network.NettyBootstrapFactory;
import org.apache.ignite.network.scalecube.ScaleCubeClusterServiceFactory;
import org.apache.ignite.raft.jraft.RaftMessagesSerializationRegistryInitializer;
import org.apache.ignite.table.manager.IgniteTables;
import org.apache.ignite.tx.IgniteTransactions;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Ignite internal implementation.
*/
public class IgniteImpl implements Ignite {
/** The logger. */
private static final IgniteLogger LOG = IgniteLogger.forClass(IgniteImpl.class);
/**
* Path to the persistent storage used by the {@link VaultService} component.
*/
private static final Path VAULT_DB_PATH = Paths.get("vault");
/**
* Path to the persistent storage used by the {@link MetaStorageManager} component.
*/
private static final Path METASTORAGE_DB_PATH = Paths.get("metastorage");
/**
* Path for the partitions persistent storage.
*/
private static final Path PARTITIONS_STORE_PATH = Paths.get("db");
/** Ignite node name. */
private final String name;
/** Vault manager. */
private final VaultManager vaultMgr;
/** Sql query engine. */
private final SqlQueryProcessor qryEngine;
/** Configuration manager that handles node (local) configuration. */
private final ConfigurationManager nodeCfgMgr;
/** Cluster service (cluster network manager). */
private final ClusterService clusterSvc;
/** Netty bootstrap factory. */
private final NettyBootstrapFactory nettyBootstrapFactory;
/** Raft manager. */
private final Loza raftMgr;
/** Meta storage manager. */
private final MetaStorageManager metaStorageMgr;
/** Configuration manager that handles cluster (distributed) configuration. */
private final ConfigurationManager clusterCfgMgr;
/** Baseline manager. */
private final BaselineManager baselineMgr;
/** Transactions manager. */
private final TxManager txManager;
/** Distributed table manager. */
private final TableManager distributedTblMgr;
/** Rest module. */
private final RestModule restModule;
/** Client handler module. */
private final ClientHandlerModule clientHandlerModule;
/** Node status. Adds ability to stop currently starting node. */
private final AtomicReference<Status> status = new AtomicReference<>(Status.STARTING);
/**
* The Constructor.
*
* @param name Ignite node name.
* @param workDir Work directory for the started node. Must not be {@code null}.
* @param serviceProviderClassLoader The class loader to be used to load provider-configuration files and provider
* classes, or {@code null} if the system class loader (or, failing that
* the bootstrap class loader) is to be used
*/
IgniteImpl(
String name,
Path workDir,
ClassLoader serviceProviderClassLoader
) {
this.name = name;
vaultMgr = createVault(workDir);
ConfigurationModules modules = loadConfigurationModules(serviceProviderClassLoader);
nodeCfgMgr = new ConfigurationManager(
modules.local().rootKeys(),
modules.local().validators(),
new LocalConfigurationStorage(vaultMgr),
modules.local().internalSchemaExtensions(),
modules.local().polymorphicSchemaExtensions()
);
NetworkConfiguration networkConfiguration = nodeCfgMgr.configurationRegistry().getConfiguration(NetworkConfiguration.KEY);
MessageSerializationRegistryImpl serializationRegistry = new MessageSerializationRegistryImpl();
RaftMessagesSerializationRegistryInitializer.registerFactories(serializationRegistry);
SqlQueryMessagesSerializationRegistryInitializer.registerFactories(serializationRegistry);
TxMessagesSerializationRegistryInitializer.registerFactories(serializationRegistry);
var clusterLocalConfiguration = new ClusterLocalConfiguration(name, serializationRegistry);
nettyBootstrapFactory = new NettyBootstrapFactory(networkConfiguration, clusterLocalConfiguration.getName());
clusterSvc = new ScaleCubeClusterServiceFactory().createClusterService(
clusterLocalConfiguration,
networkConfiguration,
nettyBootstrapFactory
);
raftMgr = new Loza(clusterSvc, workDir);
txManager = new TableTxManagerImpl(clusterSvc, new HeapLockManager());
metaStorageMgr = new MetaStorageManager(
vaultMgr,
nodeCfgMgr,
clusterSvc,
raftMgr,
new RocksDbKeyValueStorage(workDir.resolve(METASTORAGE_DB_PATH))
);
ConfigurationStorage cfgStorage = new DistributedConfigurationStorage(metaStorageMgr, vaultMgr);
clusterCfgMgr = new ConfigurationManager(
modules.distributed().rootKeys(),
modules.distributed().validators(),
cfgStorage,
modules.distributed().internalSchemaExtensions(),
modules.distributed().polymorphicSchemaExtensions()
);
baselineMgr = new BaselineManager(
clusterCfgMgr,
metaStorageMgr,
clusterSvc
);
Consumer<Consumer<Long>> registry = (c) -> {
clusterCfgMgr.configurationRegistry().listenUpdateStorageRevision(newStorageRevision -> {
c.accept(newStorageRevision);
return CompletableFuture.completedFuture(null);
});
};
distributedTblMgr = new TableManager(
registry,
clusterCfgMgr.configurationRegistry().getConfiguration(TablesConfiguration.KEY),
clusterCfgMgr.configurationRegistry().getConfiguration(DataStorageConfiguration.KEY),
raftMgr,
baselineMgr,
clusterSvc.topologyService(),
getPartitionsStorePath(workDir),
txManager
);
qryEngine = new SqlQueryProcessor(
registry,
clusterSvc,
distributedTblMgr
);
restModule = new RestModule(nodeCfgMgr, clusterCfgMgr, nettyBootstrapFactory);
clientHandlerModule = new ClientHandlerModule(
qryEngine,
distributedTblMgr,
new IgniteTransactionsImpl(txManager),
nodeCfgMgr.configurationRegistry(),
nettyBootstrapFactory
);
}
private ConfigurationModules loadConfigurationModules(ClassLoader classLoader) {
var modulesProvider = new ServiceLoaderModulesProvider();
List<ConfigurationModule> modules = modulesProvider.modules(classLoader);
if (LOG.isInfoEnabled()) {
LOG.info("Configuration modules loaded: {}", modules);
}
if (modules.isEmpty()) {
throw new IllegalStateException("No configuration modules were loaded, this means Ignite cannot start. "
+ "Please make sure that the classloader for loading services is correct.");
}
var configModules = new ConfigurationModules(modules);
if (LOG.isInfoEnabled()) {
LOG.info("Local root keys: {}", configModules.local().rootKeys());
LOG.info("Distributed root keys: {}", configModules.distributed().rootKeys());
}
return configModules;
}
/**
* Starts ignite node.
*
* @param cfg Optional node configuration based on {@link org.apache.ignite.configuration.schemas.runner.NodeConfigurationSchema} and
* {@link org.apache.ignite.configuration.schemas.network.NetworkConfigurationSchema}. Following rules are used for applying
* the configuration properties:
* <ol>
* <li>Specified property overrides existing one or just applies itself if it wasn't
* previously specified.</li>
* <li>All non-specified properties either use previous value or use default one from
* corresponding configuration schema.</li>
* </ol>
* So that, in case of initial node start (first start ever) specified configuration, supplemented with defaults, is
* used. If no configuration was provided defaults are used for all configuration properties. In case of node
* restart, specified properties override existing ones, non specified properties that also weren't specified
* previously use default values. Please pay attention that previously specified properties are searched in the
* {@code workDir} specified by the user.
*/
public void start(@Nullable String cfg) {
List<IgniteComponent> startedComponents = new ArrayList<>();
try {
// Vault startup.
doStartComponent(
name,
startedComponents,
vaultMgr
);
vaultMgr.putName(name).join();
// Node configuration manager startup.
doStartComponent(
name,
startedComponents,
nodeCfgMgr);
// Node configuration manager bootstrap.
if (cfg != null) {
try {
nodeCfgMgr.bootstrap(cfg);
} catch (Exception e) {
throw new IgniteException("Unable to parse user-specific configuration.", e);
}
} else {
nodeCfgMgr.configurationRegistry().initializeDefaults();
}
// Start the remaining components.
List<IgniteComponent> otherComponents = List.of(
nettyBootstrapFactory,
clusterSvc,
raftMgr,
txManager,
metaStorageMgr,
clusterCfgMgr,
baselineMgr,
distributedTblMgr,
qryEngine,
restModule,
clientHandlerModule
);
for (IgniteComponent component : otherComponents) {
doStartComponent(name, startedComponents, component);
}
notifyConfigurationListeners();
// Deploy all registered watches because all components are ready and have registered their listeners.
metaStorageMgr.deployWatches();
if (!status.compareAndSet(Status.STARTING, Status.STARTED)) {
throw new NodeStoppingException();
}
} catch (Exception e) {
String errMsg = "Unable to start node=[" + name + "].";
LOG.error(errMsg, e);
doStopNode(startedComponents);
throw new IgniteException(errMsg, e);
}
}
/**
* Stops ignite node.
*/
public void stop() {
if (status.getAndSet(Status.STOPPING) == Status.STARTED) {
doStopNode(List.of(vaultMgr, nodeCfgMgr, clusterSvc, raftMgr, txManager, metaStorageMgr, clusterCfgMgr, baselineMgr,
distributedTblMgr, qryEngine, restModule, clientHandlerModule, nettyBootstrapFactory));
}
}
/** {@inheritDoc} */
@Override
public IgniteTables tables() {
return distributedTblMgr;
}
public QueryProcessor queryEngine() {
return qryEngine;
}
/** {@inheritDoc} */
@Override
public IgniteTransactions transactions() {
return new IgniteTransactionsImpl(txManager);
}
/** {@inheritDoc} */
@Override
public void close() {
IgnitionManager.stop(name);
}
/** {@inheritDoc} */
@Override
public String name() {
return name;
}
/** {@inheritDoc} */
@Override
public void setBaseline(Set<String> baselineNodes) {
try {
distributedTblMgr.setBaseline(baselineNodes);
} catch (NodeStoppingException e) {
throw new IgniteException(e);
}
}
/**
* Returns node configuration.
*/
public ConfigurationRegistry nodeConfiguration() {
return nodeCfgMgr.configurationRegistry();
}
/**
* Returns cluster configuration.
*/
public ConfigurationRegistry clusterConfiguration() {
return clusterCfgMgr.configurationRegistry();
}
/**
* Returns client handler module.
*/
public ClientHandlerModule clientHandlerModule() {
return clientHandlerModule;
}
/**
* Returns the id of the current node.
*/
public String id() {
return clusterSvc.topologyService().localMember().id();
}
/**
* Checks node status. If it's {@link Status#STOPPING} then prevents further starting and throws NodeStoppingException that will lead to
* stopping already started components later on, otherwise starts component and add it to started components list.
*
* @param nodeName Node name.
* @param startedComponents List of already started components for given node.
* @param component Ignite component to start.
* @param <T> Ignite component type.
* @throws NodeStoppingException If node stopping intention was detected.
*/
private <T extends IgniteComponent> void doStartComponent(
@NotNull String nodeName,
@NotNull List<IgniteComponent> startedComponents,
@NotNull T component
) throws NodeStoppingException {
if (status.get() == Status.STOPPING) {
throw new NodeStoppingException("Node=[" + nodeName + "] was stopped.");
} else {
startedComponents.add(component);
component.start();
}
}
/**
* Calls {@link IgniteComponent#beforeNodeStop()} and then {@link IgniteComponent#stop()} for all components in start-reverse-order.
* Cleanups node started components map and node status map.
*
* @param startedComponents List of already started components for given node.
*/
private void doStopNode(@NotNull List<IgniteComponent> startedComponents) {
ListIterator<IgniteComponent> beforeStopIter =
startedComponents.listIterator(startedComponents.size());
while (beforeStopIter.hasPrevious()) {
IgniteComponent componentToExecBeforeNodeStop = beforeStopIter.previous();
try {
componentToExecBeforeNodeStop.beforeNodeStop();
} catch (Exception e) {
LOG.error("Unable to execute before node stop on the component=["
+ componentToExecBeforeNodeStop + "] within node=[" + name + ']', e);
}
}
ListIterator<IgniteComponent> stopIter =
startedComponents.listIterator(startedComponents.size());
while (stopIter.hasPrevious()) {
IgniteComponent componentToStop = stopIter.previous();
try {
componentToStop.stop();
} catch (Exception e) {
LOG.error("Unable to stop component=[" + componentToStop + "] within node=[" + name + ']', e);
}
}
}
/**
* Notify all listeners of current configurations.
*
* @throws Exception If failed.
*/
private void notifyConfigurationListeners() throws Exception {
CompletableFuture.allOf(
nodeConfiguration().notifyCurrentConfigurationListeners(),
clusterConfiguration().notifyCurrentConfigurationListeners()
).get();
}
/**
* Starts the Vault component.
*/
private static VaultManager createVault(Path workDir) {
Path vaultPath = workDir.resolve(VAULT_DB_PATH);
try {
Files.createDirectories(vaultPath);
} catch (IOException e) {
throw new IgniteInternalException(e);
}
return new VaultManager(new PersistentVaultService(vaultPath));
}
/**
* Returns a path to the partitions store directory. Creates a directory if it doesn't exist.
*
* @param workDir Ignite work directory.
* @return Partitions store path.
*/
@NotNull
private static Path getPartitionsStorePath(Path workDir) {
Path partitionsStore = workDir.resolve(PARTITIONS_STORE_PATH);
try {
Files.createDirectories(partitionsStore);
} catch (IOException e) {
throw new IgniteInternalException("Failed to create directory for partitions storage: " + e.getMessage(), e);
}
return partitionsStore;
}
/**
* Node state.
*/
private enum Status {
STARTING,
STARTED,
STOPPING
}
}