-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
CommandHandler.java
1868 lines (1442 loc) · 59.7 KB
/
CommandHandler.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
995
996
997
998
999
1000
/*
* 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.commandline;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
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.Scanner;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;
import org.apache.ignite.IgniteSystemProperties;
import org.apache.ignite.cluster.ClusterNode;
import org.apache.ignite.internal.client.GridClient;
import org.apache.ignite.internal.client.GridClientAuthenticationException;
import org.apache.ignite.internal.client.GridClientClosedException;
import org.apache.ignite.internal.client.GridClientClusterState;
import org.apache.ignite.internal.client.GridClientCompute;
import org.apache.ignite.internal.client.GridClientConfiguration;
import org.apache.ignite.internal.client.GridClientDisconnectedException;
import org.apache.ignite.internal.client.GridClientException;
import org.apache.ignite.internal.client.GridClientFactory;
import org.apache.ignite.internal.client.GridClientHandshakeException;
import org.apache.ignite.internal.client.GridClientNode;
import org.apache.ignite.internal.client.GridServerUnreachableException;
import org.apache.ignite.internal.client.impl.connection.GridClientConnectionResetException;
import org.apache.ignite.internal.commandline.cache.CacheArguments;
import org.apache.ignite.internal.commandline.cache.CacheCommand;
import org.apache.ignite.internal.processors.cache.verify.CacheInfo;
import org.apache.ignite.internal.processors.cache.verify.ContentionInfo;
import org.apache.ignite.internal.processors.cache.verify.PartitionHashRecord;
import org.apache.ignite.internal.processors.cache.verify.PartitionKey;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.internal.util.typedef.X;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.internal.visor.VisorTaskArgument;
import org.apache.ignite.internal.visor.baseline.VisorBaselineNode;
import org.apache.ignite.internal.visor.baseline.VisorBaselineOperation;
import org.apache.ignite.internal.visor.baseline.VisorBaselineTask;
import org.apache.ignite.internal.visor.baseline.VisorBaselineTaskArg;
import org.apache.ignite.internal.visor.baseline.VisorBaselineTaskResult;
import org.apache.ignite.internal.visor.misc.VisorClusterNode;
import org.apache.ignite.internal.visor.misc.VisorWalTask;
import org.apache.ignite.internal.visor.misc.VisorWalTaskArg;
import org.apache.ignite.internal.visor.misc.VisorWalTaskOperation;
import org.apache.ignite.internal.visor.misc.VisorWalTaskResult;
import org.apache.ignite.internal.visor.tx.VisorTxInfo;
import org.apache.ignite.internal.visor.tx.VisorTxOperation;
import org.apache.ignite.internal.visor.tx.VisorTxProjection;
import org.apache.ignite.internal.visor.tx.VisorTxSortOrder;
import org.apache.ignite.internal.visor.tx.VisorTxTask;
import org.apache.ignite.internal.visor.tx.VisorTxTaskArg;
import org.apache.ignite.internal.visor.tx.VisorTxTaskResult;
import org.apache.ignite.internal.visor.verify.IndexValidationIssue;
import org.apache.ignite.internal.visor.verify.ValidateIndexesPartitionResult;
import org.apache.ignite.internal.visor.verify.VisorContentionTask;
import org.apache.ignite.internal.visor.verify.VisorContentionTaskArg;
import org.apache.ignite.internal.visor.verify.VisorContentionTaskResult;
import org.apache.ignite.internal.visor.verify.VisorIdleVerifyTask;
import org.apache.ignite.internal.visor.verify.VisorIdleVerifyTaskArg;
import org.apache.ignite.internal.visor.verify.VisorIdleVerifyTaskResult;
import org.apache.ignite.internal.visor.verify.VisorValidateIndexesJobResult;
import org.apache.ignite.internal.visor.verify.VisorValidateIndexesTaskArg;
import org.apache.ignite.internal.visor.verify.VisorValidateIndexesTaskResult;
import org.apache.ignite.internal.visor.verify.VisorViewCacheTask;
import org.apache.ignite.internal.visor.verify.VisorViewCacheTaskArg;
import org.apache.ignite.internal.visor.verify.VisorViewCacheTaskResult;
import org.apache.ignite.lang.IgniteClosure;
import org.apache.ignite.plugin.security.SecurityCredentials;
import org.apache.ignite.plugin.security.SecurityCredentialsBasicProvider;
import static org.apache.ignite.IgniteSystemProperties.IGNITE_ENABLE_EXPERIMENTAL_COMMAND;
import static org.apache.ignite.internal.IgniteVersionUtils.ACK_VER_STR;
import static org.apache.ignite.internal.IgniteVersionUtils.COPYRIGHT;
import static org.apache.ignite.internal.commandline.Command.ACTIVATE;
import static org.apache.ignite.internal.commandline.Command.BASELINE;
import static org.apache.ignite.internal.commandline.Command.CACHE;
import static org.apache.ignite.internal.commandline.Command.DEACTIVATE;
import static org.apache.ignite.internal.commandline.Command.STATE;
import static org.apache.ignite.internal.commandline.Command.TX;
import static org.apache.ignite.internal.commandline.Command.WAL;
import static org.apache.ignite.internal.visor.baseline.VisorBaselineOperation.ADD;
import static org.apache.ignite.internal.visor.baseline.VisorBaselineOperation.COLLECT;
import static org.apache.ignite.internal.visor.baseline.VisorBaselineOperation.REMOVE;
import static org.apache.ignite.internal.visor.baseline.VisorBaselineOperation.SET;
import static org.apache.ignite.internal.visor.baseline.VisorBaselineOperation.VERSION;
import static org.apache.ignite.internal.visor.verify.VisorViewCacheCmd.GROUPS;
import static org.apache.ignite.internal.visor.verify.VisorViewCacheCmd.SEQ;
/**
* Class that execute several commands passed via command line.
*/
public class CommandHandler {
/** Logger. */
private static final Logger log = Logger.getLogger(CommandHandler.class.getName());
/** */
static final String DFLT_HOST = "127.0.0.1";
/** */
static final String DFLT_PORT = "11211";
/** */
private static final String CMD_HELP = "--help";
/** */
private static final String CMD_HOST = "--host";
/** */
private static final String CMD_PORT = "--port";
/** */
private static final String CMD_PASSWORD = "--password";
/** */
private static final String CMD_USER = "--user";
/** Force option is used for auto confirmation. */
private static final String CMD_FORCE = "--force";
/** */
protected static final String CMD_PING_INTERVAL = "--ping-interval";
/** */
protected static final String CMD_PING_TIMEOUT = "--ping-timeout";
/** List of optional auxiliary commands. */
private static final Set<String> AUX_COMMANDS = new HashSet<>();
static {
AUX_COMMANDS.add(CMD_HELP);
AUX_COMMANDS.add(CMD_HOST);
AUX_COMMANDS.add(CMD_PORT);
AUX_COMMANDS.add(CMD_PASSWORD);
AUX_COMMANDS.add(CMD_USER);
AUX_COMMANDS.add(CMD_FORCE);
AUX_COMMANDS.add(CMD_PING_INTERVAL);
AUX_COMMANDS.add(CMD_PING_TIMEOUT);
}
/** Broadcast uuid. */
private static final UUID BROADCAST_UUID = UUID.randomUUID();
/** */
public static final String CONFIRM_MSG = "y";
/** */
private static final String BASELINE_ADD = "add";
/** */
private static final String BASELINE_REMOVE = "remove";
/** */
private static final String BASELINE_COLLECT = "collect";
/** */
private static final String BASELINE_SET = "set";
/** */
private static final String BASELINE_SET_VERSION = "version";
/** Parameter name for validate_indexes command. */
static final String VI_CHECK_FIRST = "checkFirst";
/** Parameter name for validate_indexes command. */
static final String VI_CHECK_THROUGH = "checkThrough";
/** */
static final String WAL_PRINT = "print";
/** */
static final String WAL_DELETE = "delete";
/** */
static final String DELIM = "--------------------------------------------------------------------------------";
/** */
public static final int EXIT_CODE_OK = 0;
/** */
public static final int EXIT_CODE_INVALID_ARGUMENTS = 1;
/** */
public static final int EXIT_CODE_CONNECTION_FAILED = 2;
/** */
public static final int ERR_AUTHENTICATION_FAILED = 3;
/** */
public static final int EXIT_CODE_UNEXPECTED_ERROR = 4;
/** */
private static final long DFLT_PING_INTERVAL = 5000L;
/** */
private static final long DFLT_PING_TIMEOUT = 30_000L;
/** */
private static final Scanner IN = new Scanner(System.in);
/** Validate indexes task name. */
private static final String VALIDATE_INDEXES_TASK = "org.apache.ignite.internal.visor.verify.VisorValidateIndexesTask";
/** */
private static final String TX_LIMIT = "limit";
/** */
private static final String TX_ORDER = "order";
/** */
public static final String CMD_TX_ORDER_START_TIME="START_TIME";
/** */
private static final String TX_SERVERS = "servers";
/** */
private static final String TX_CLIENTS = "clients";
/** */
private static final String TX_DURATION = "minDuration";
/** */
private static final String TX_SIZE = "minSize";
/** */
private static final String TX_LABEL = "label";
/** */
private static final String TX_NODES = "nodes";
/** */
private static final String TX_XID = "xid";
/** */
private static final String TX_KILL = "kill";
/** */
private Iterator<String> argsIt;
/** */
private String peekedArg;
/** */
private Object lastOperationRes;
/** */
private GridClientConfiguration clientCfg;
/** Check if experimental commands are enabled. Default {@code false}. */
private final boolean enableExperimental = IgniteSystemProperties.getBoolean(IGNITE_ENABLE_EXPERIMENTAL_COMMAND, false);
/**
* Output specified string to console.
*
* @param s String to output.
*/
private void log(String s) {
System.out.println(s);
}
/**
* Provides a prompt, then reads a single line of text from the console.
*
* @param prompt text
* @return A string containing the line read from the console
*/
private String readLine(String prompt) {
System.out.print(prompt);
return IN.nextLine();
}
/**
* Output empty line.
*/
private void nl() {
System.out.println("");
}
/**
* Print error to console.
*
* @param errCode Error code to return.
* @param s Optional message.
* @param e Error to print.
*/
private int error(int errCode, String s, Throwable e) {
if (!F.isEmpty(s))
log(s);
String msg = e.getMessage();
if (F.isEmpty(msg))
msg = e.getClass().getName();
if (msg.startsWith("Failed to handle request")) {
int p = msg.indexOf("err=");
msg = msg.substring(p + 4, msg.length() - 1);
}
log("Error: " + msg);
return errCode;
}
/**
* Requests interactive user confirmation if forthcoming operation is dangerous.
*
* @param args Arguments.
* @return {@code true} if operation confirmed (or not needed), {@code false} otherwise.
*/
private boolean confirm(Arguments args) {
String prompt = confirmationPrompt(args);
if (prompt == null)
return true;
return CONFIRM_MSG.equalsIgnoreCase(readLine(prompt));
}
/**
* @param args Arguments.
* @return Prompt text if confirmation needed, otherwise {@code null}.
*/
private String confirmationPrompt(Arguments args) {
if (args.force())
return null;
String str = null;
switch (args.command()) {
case DEACTIVATE:
str = "Warning: the command will deactivate a cluster.";
break;
case BASELINE:
if (!BASELINE_COLLECT.equals(args.baselineAction()))
str = "Warning: the command will perform changes in baseline.";
break;
case WAL:
if (WAL_DELETE.equals(args.walAction()))
str = "Warning: the command will delete unused WAL segments.";
break;
case TX:
if (args.transactionArguments().getOperation() == VisorTxOperation.KILL)
str = "Warning: the command will kill some transactions.";
break;
default:
break;
}
return str == null ? null : str + "\nPress '" + CONFIRM_MSG + "' to continue . . . ";
}
/**
* @param rawArgs Arguments.
*/
private void initArgIterator(List<String> rawArgs) {
argsIt = rawArgs.iterator();
peekedArg = null;
}
/**
* @return Returns {@code true} if the iteration has more elements.
*/
private boolean hasNextArg() {
return peekedArg != null || argsIt.hasNext();
}
/**
* Activate cluster.
*
* @param client Client.
* @throws GridClientException If failed to activate.
*/
private void activate(GridClient client) throws Throwable {
try {
GridClientClusterState state = client.state();
state.active(true);
log("Cluster activated");
}
catch (Throwable e) {
log("Failed to activate cluster.");
throw e;
}
}
/**
* Deactivate cluster.
*
* @param client Client.
* @throws Throwable If failed to deactivate.
*/
private void deactivate(GridClient client) throws Throwable {
try {
GridClientClusterState state = client.state();
state.active(false);
log("Cluster deactivated");
}
catch (Throwable e) {
log("Failed to deactivate cluster.");
throw e;
}
}
/**
* Print cluster state.
*
* @param client Client.
* @throws Throwable If failed to print state.
*/
private void state(GridClient client) throws Throwable {
try {
GridClientClusterState state = client.state();
log("Cluster is " + (state.active() ? "active" : "inactive"));
}
catch (Throwable e) {
log("Failed to get cluster state.");
throw e;
}
}
/**
*
* @param client Client.
* @param taskCls Task class.
* @param taskArgs Task arguments.
* @return Task result.
* @throws GridClientException If failed to execute task.
*/
private <R> R executeTask(GridClient client, Class<?> taskCls, Object taskArgs) throws GridClientException {
return executeTaskByNameOnNode(client, taskCls.getName(), taskArgs, null);
}
/**
* @param client Client
* @param taskClsName Task class name.
* @param taskArgs Task args.
* @param nodeId Node ID to execute task at (if null, random node will be chosen by balancer).
* @return Task result.
* @throws GridClientException If failed to execute task.
*/
private <R> R executeTaskByNameOnNode(GridClient client, String taskClsName, Object taskArgs, UUID nodeId
) throws GridClientException {
GridClientCompute compute = client.compute();
if (nodeId == BROADCAST_UUID) {
Collection<GridClientNode> nodes = compute.nodes(GridClientNode::connectable);
if (F.isEmpty(nodes))
throw new GridClientDisconnectedException("Connectable nodes not found", null);
List<UUID> nodeIds = nodes.stream()
.map(GridClientNode::nodeId)
.collect(Collectors.toList());
return client.compute().execute(taskClsName, new VisorTaskArgument<>(nodeIds, taskArgs, false));
}
GridClientNode node = null;
if (nodeId == null) {
Collection<GridClientNode> nodes = compute.nodes(GridClientNode::connectable);
// Prefer node from connect string.
String origAddr = clientCfg.getServers().iterator().next();
for (GridClientNode clientNode : nodes) {
Iterator<String> it = F.concat(clientNode.tcpAddresses().iterator(), clientNode.tcpHostNames().iterator());
while (it.hasNext()) {
if (origAddr.equals(it.next() + ":" + clientNode.tcpPort())) {
node = clientNode;
break;
}
}
}
// Otherwise choose random node.
if (node == null)
node = getBalancedNode(compute);
}
else {
for (GridClientNode n : compute.nodes()) {
if (n.connectable() && nodeId.equals(n.nodeId())) {
node = n;
break;
}
}
if (node == null)
throw new IllegalArgumentException("Node with id=" + nodeId + " not found");
}
return compute.projection(node).execute(taskClsName, new VisorTaskArgument<>(node.nodeId(), taskArgs, false));
}
/**
* @param compute instance
* @return balanced node
*/
private GridClientNode getBalancedNode(GridClientCompute compute) throws GridClientException {
Collection<GridClientNode> nodes = compute.nodes(GridClientNode::connectable);
if (F.isEmpty(nodes))
throw new GridClientDisconnectedException("Connectable node not found", null);
return compute.balancer().balancedNode(nodes);
}
/**
* Executes --cache subcommand.
*
* @param client Client.
* @param cacheArgs Cache args.
*/
private void cache(GridClient client, CacheArguments cacheArgs) throws Throwable {
switch (cacheArgs.command()) {
case HELP:
printCacheHelp();
break;
case IDLE_VERIFY:
cacheIdleVerify(client, cacheArgs);
break;
case VALIDATE_INDEXES:
cacheValidateIndexes(client, cacheArgs);
break;
case CONTENTION:
cacheContention(client, cacheArgs);
break;
default:
cacheView(client, cacheArgs);
break;
}
}
/**
*
*/
private void printCacheHelp() {
log("--cache subcommand allows to do the following operations:");
usage(" Show information about caches, groups or sequences that match a regex:", CACHE, " list regexPattern [groups|seq] [nodeId]");
usage(" Show hot keys that are point of contention for multiple transactions:", CACHE, " contention minQueueSize [nodeId] [maxPrint]");
usage(" Verify partition counters and hashes between primary and backups on idle cluster:", CACHE, " idle_verify [cache1,...,cacheN]");
usage(" Validate custom indexes on idle cluster:", CACHE, " validate_indexes [cache1,...,cacheN] [nodeId] [checkFirst|checkThrough]");
log(" If [nodeId] is not specified, contention and validate_indexes commands will be broadcasted to all server nodes.");
log(" Another commands where [nodeId] is optional will run on a random server node.");
log(" checkFirst numeric parameter for validate_indexes specifies number of first K keys to be validated.");
log(" checkThrough numeric parameter for validate_indexes allows to check each Kth key.");
nl();
}
/**
* @param client Client.
* @param cacheArgs Cache args.
*/
private void cacheContention(GridClient client, CacheArguments cacheArgs) throws GridClientException {
VisorContentionTaskArg taskArg = new VisorContentionTaskArg(
cacheArgs.minQueueSize(), cacheArgs.maxPrint());
UUID nodeId = cacheArgs.nodeId() == null ? BROADCAST_UUID : cacheArgs.nodeId();
VisorContentionTaskResult res = executeTaskByNameOnNode(
client, VisorContentionTask.class.getName(), taskArg, nodeId);
if (!F.isEmpty(res.exceptions())) {
log("Contention check failed on nodes:");
for (Map.Entry<UUID, Exception> e : res.exceptions().entrySet()) {
log("Node ID = " + e.getKey());
log("Exception message:");
log(e.getValue().getMessage());
nl();
}
}
for (ContentionInfo info : res.getInfos())
info.print();
}
/**
* @param client Client.
* @param cacheArgs Cache args.
*/
private void cacheValidateIndexes(GridClient client, CacheArguments cacheArgs) throws GridClientException {
VisorValidateIndexesTaskArg taskArg = new VisorValidateIndexesTaskArg(
cacheArgs.caches(),
cacheArgs.checkFirst(),
cacheArgs.checkThrough()
);
UUID nodeId = cacheArgs.nodeId() == null ? BROADCAST_UUID : cacheArgs.nodeId();
VisorValidateIndexesTaskResult taskRes = executeTaskByNameOnNode(
client, VALIDATE_INDEXES_TASK, taskArg, nodeId);
if (!F.isEmpty(taskRes.exceptions())) {
log("Index validation failed on nodes:");
for (Map.Entry<UUID, Exception> e : taskRes.exceptions().entrySet()) {
log("Node ID = " + e.getKey());
log("Exception message:");
log(e.getValue().getMessage());
nl();
}
}
boolean errors = false;
for (Map.Entry<UUID, VisorValidateIndexesJobResult> nodeEntry : taskRes.results().entrySet()) {
Map<PartitionKey, ValidateIndexesPartitionResult> partRes = nodeEntry.getValue().partitionResult();
for (Map.Entry<PartitionKey, ValidateIndexesPartitionResult> e : partRes.entrySet()) {
ValidateIndexesPartitionResult res = e.getValue();
if (!res.issues().isEmpty()) {
errors = true;
log(e.getKey().toString() + " " + e.getValue().toString());
for (IndexValidationIssue is : res.issues())
log(is.toString());
}
}
Map<String, ValidateIndexesPartitionResult> idxRes = nodeEntry.getValue().indexResult();
for (Map.Entry<String, ValidateIndexesPartitionResult> e : idxRes.entrySet()) {
ValidateIndexesPartitionResult res = e.getValue();
if (!res.issues().isEmpty()) {
errors = true;
log("SQL Index " + e.getKey() + " " + e.getValue().toString());
for (IndexValidationIssue is : res.issues())
log(is.toString());
}
}
}
if (!errors)
log("validate_indexes has finished, no issues found.");
else
log("validate_indexes has finished with errors (listed above).");
}
/**
* @param client Client.
* @param cacheArgs Cache args.
*/
private void cacheView(GridClient client, CacheArguments cacheArgs) throws GridClientException {
VisorViewCacheTaskArg taskArg = new VisorViewCacheTaskArg(cacheArgs.regex(), cacheArgs.cacheCommand());
VisorViewCacheTaskResult res = executeTaskByNameOnNode(
client, VisorViewCacheTask.class.getName(), taskArg, cacheArgs.nodeId());
for (CacheInfo info : res.cacheInfos())
info.print(cacheArgs.cacheCommand());
}
/**
* @param client Client.
* @param cacheArgs Cache args.
*/
private void cacheIdleVerify(GridClient client, CacheArguments cacheArgs) throws GridClientException {
VisorIdleVerifyTaskResult res = executeTask(
client, VisorIdleVerifyTask.class, new VisorIdleVerifyTaskArg(cacheArgs.caches()));
Map<PartitionKey, List<PartitionHashRecord>> conflicts = res.getConflicts();
if (conflicts.isEmpty()) {
log("idle_verify check has finished, no conflicts have been found.");
nl();
}
else {
log ("idle_verify check has finished, found " + conflicts.size() + " conflict partitions.");
nl();
for (Map.Entry<PartitionKey, List<PartitionHashRecord>> entry : conflicts.entrySet()) {
log("Conflict partition: " + entry.getKey());
log("Partition instances: " + entry.getValue());
}
}
}
/**
* Change baseline.
*
* @param client Client.
* @param baselineAct Baseline action to execute. @throws GridClientException If failed to execute baseline action.
* @param baselineArgs Baseline action arguments.
* @throws Throwable If failed to execute baseline action.
*/
private void baseline(GridClient client, String baselineAct, String baselineArgs) throws Throwable {
switch (baselineAct) {
case BASELINE_ADD:
baselineAdd(client, baselineArgs);
break;
case BASELINE_REMOVE:
baselineRemove(client, baselineArgs);
break;
case BASELINE_SET:
baselineSet(client, baselineArgs);
break;
case BASELINE_SET_VERSION:
baselineVersion(client, baselineArgs);
break;
case BASELINE_COLLECT:
baselinePrint(client);
break;
}
}
/**
* Prepare task argument.
*
* @param op Operation.
* @param s Argument from command line.
* @return Task argument.
*/
private VisorBaselineTaskArg arg(VisorBaselineOperation op, String s) {
switch (op) {
case ADD:
case REMOVE:
case SET:
List<String> consistentIds = getConsistentIds(s);
return new VisorBaselineTaskArg(op, -1, consistentIds);
case VERSION:
try {
long topVer = Long.parseLong(s);
return new VisorBaselineTaskArg(op, topVer, null);
}
catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid topology version: " + s, e);
}
default:
return new VisorBaselineTaskArg(op, -1, null);
}
}
/**
* @param s String of consisted ids delimited by comma.
* @return List of consistent ids.
*/
private List<String> getConsistentIds(String s) {
if (F.isEmpty(s))
throw new IllegalArgumentException("Empty list of consistent IDs");
List<String> consistentIds = new ArrayList<>();
for (String consistentId : s.split(","))
consistentIds.add(consistentId.trim());
return consistentIds;
}
/**
* Print baseline topology.
*
* @param res Task result with baseline topology.
*/
private void baselinePrint0(VisorBaselineTaskResult res) {
log("Cluster state: " + (res.isActive() ? "active" : "inactive"));
log("Current topology version: " + res.getTopologyVersion());
nl();
Map<String, VisorBaselineNode> baseline = res.getBaseline();
Map<String, VisorBaselineNode> srvs = res.getServers();
if (F.isEmpty(baseline))
log("Baseline nodes not found.");
else {
log("Baseline nodes:");
for(VisorBaselineNode node : baseline.values()) {
log(" ConsistentID=" + node.getConsistentId() + ", STATE=" +
(srvs.containsKey(node.getConsistentId()) ? "ONLINE" : "OFFLINE"));
}
log(DELIM);
log("Number of baseline nodes: " + baseline.size());
nl();
List<VisorBaselineNode> others = new ArrayList<>();
for (VisorBaselineNode node : srvs.values()) {
if (!baseline.containsKey(node.getConsistentId()))
others.add(node);
}
if (F.isEmpty(others))
log("Other nodes not found.");
else {
log("Other nodes:");
for(VisorBaselineNode node : others)
log(" ConsistentID=" + node.getConsistentId());
log("Number of other nodes: " + others.size());
}
}
}
/**
* Print current baseline.
*
* @param client Client.
*/
private void baselinePrint(GridClient client) throws GridClientException {
VisorBaselineTaskResult res = executeTask(client, VisorBaselineTask.class, arg(COLLECT, ""));
baselinePrint0(res);
}
/**
* Add nodes to baseline.
*
* @param client Client.
* @param baselineArgs Baseline action arguments.
* @throws Throwable If failed to add nodes to baseline.
*/
private void baselineAdd(GridClient client, String baselineArgs) throws Throwable {
try {
VisorBaselineTaskResult res = executeTask(client, VisorBaselineTask.class, arg(ADD, baselineArgs));
baselinePrint0(res);
}
catch (Throwable e) {
log("Failed to add nodes to baseline.");
throw e;
}
}
/**
* Remove nodes from baseline.
*
* @param client Client.
* @param consistentIds Consistent IDs.
* @throws Throwable If failed to remove nodes from baseline.
*/
private void baselineRemove(GridClient client, String consistentIds) throws Throwable {
try {
VisorBaselineTaskResult res = executeTask(client, VisorBaselineTask.class, arg(REMOVE, consistentIds));
baselinePrint0(res);
}
catch (Throwable e) {
log("Failed to remove nodes from baseline.");
throw e;
}
}
/**
* Set baseline.
*
* @param client Client.
* @param consistentIds Consistent IDs.
* @throws Throwable If failed to set baseline.
*/
private void baselineSet(GridClient client, String consistentIds) throws Throwable {
try {
VisorBaselineTaskResult res = executeTask(client, VisorBaselineTask.class, arg(SET, consistentIds));
baselinePrint0(res);
}
catch (Throwable e) {
log("Failed to set baseline.");
throw e;
}
}
/**
* Set baseline by topology version.
*
* @param client Client.
* @param arg Argument from command line.
*/
private void baselineVersion(GridClient client, String arg) throws GridClientException {
try {
VisorBaselineTaskResult res = executeTask(client, VisorBaselineTask.class, arg(VERSION, arg));
baselinePrint0(res);
}
catch (Throwable e) {
log("Failed to set baseline with specified topology version.");
throw e;
}
}
/**
* Dump transactions information.
*
* @param client Client.
* @param arg Transaction search arguments
*/
private void transactions(GridClient client, VisorTxTaskArg arg) throws GridClientException {
try {
Map<ClusterNode, VisorTxTaskResult> res = executeTask(client, VisorTxTask.class, arg);
lastOperationRes = res;
if (res.isEmpty())
log("Nothing found.");
else if (arg.getOperation() == VisorTxOperation.KILL)
log("Killed transactions:");
else
log("Matching transactions:");
for (Map.Entry<ClusterNode, VisorTxTaskResult> entry : res.entrySet()) {
if (entry.getValue().getInfos().isEmpty())
continue;
ClusterNode key = entry.getKey();
log(key.toString());
for (VisorTxInfo info : entry.getValue().getInfos())
log(" Tx: [xid=" + info.getXid() +
", label=" + info.getLabel() +
", state=" + info.getState() +
", startTime=" + info.getFormattedStartTime() +
", duration=" + info.getDuration() / 1000 +
", isolation=" + info.getIsolation() +
", concurrency=" + info.getConcurrency() +
", timeout=" + info.getTimeout() +
", size=" + info.getSize() +
", dhtNodes=" + F.transform(info.getPrimaryNodes(), new IgniteClosure<UUID, String>() {
@Override public String apply(UUID id) {
return U.id8(id);